Find element in list of tuples in Python

Python Tuple

In this post, we are going to explore ways of how to Find element in list of tuples in Python with code examples.

1. List Comprehension to Find element in list of tuples in Python


  • In this code example, we are using list Comprehension to find an element from the list of tuples.
  • We loop over the list if condition item == ‘C#’ returns the matched tuples from the list of tuples. Finally, we are printing the filtered tuples result.

Code example

#Program to find an element in a list of tuples Python

list_of_tuples= [("C#", 15), ("C#", 1), ("C++", 3),("C++", 3), ("Python", 25), ("Rust", 30)]

fillter_tuples =  [ (item,value) for item, value in list_of_tuples if item  == 'C#' ]


print('found tuples from list of tuples:\n',fillter_tuples)

Output

found tuples from list of tuples:

 [('C#', 15), ('C#', 1)]

2. In operator to find element in list of tuples in Python


  • In this code example, we are using IN operator to find an element from the list of tuples.
  • We loop over the list of tuples, if condition ‘C#’ in item, returned True or false and return matched tuples from list of tuples.
  • Finally, we are printing the filtered tuples from a list of tuples

Code Example

#Program to find an element in a list of tuples Python

list_of_tuples= [("C#", 15), ("C#", 1), ("C++", 3),("C++", 3), ("Python", 25), ("Rust", 30)]

   

fillter_tuples =  [ item for item in list_of_tuples if 'C#'  in item ]


print('found tuples from list of tuples:\n',fillter_tuples)

Output

found tuples from list of tuples:

 [('C#', 15), ('C#', 1)]

3. Loop to Find element in list of tuples in Python


Here, we are loop over the list of tuples using the loop to find Find an element from the list of tuples.

Python Program Example

#Program to find an element in a list of tuples Python

list_of_tuples= [("C#", 15), ("C#", 1), ("C++", 3),("C++", 3), ("Python", 25), ("Rust", 30)]



fillter_tuples  = [tup for tup in list_of_tuples if tup[0] == 'C#']


print('found tuples from list of tuples:\n',fillter_tuples)

Output

found tuples from list of tuples:

 [('C#', 15), ('C#', 1)]

4. For loop to Find element in list of tuples in Python


Here, we are using for loop to iterate over the list_of_tuples and checking item exist in the list_of_tuples. If the item exists in a list we are appending the item in filter_tuple_list list and finally printing the filter_tuple_list from the list of tuples.

Code example

#Program to find an element in a list of tuples Python

list_of_tuples= [("C#", 15), ("C#", 1), ("C++", 3), 
     ("C++", 3), ("Python", 25), ("Rust", 30)]

filter_tuple_list = []

for item in list_of_tuples:
   if 'C#' in item:
       filter_tuple_list.append(item)


print('found tuples from list of tuples:\n',filter_tuple_list)

Output

found tuples from list of tuples:

 [('C#', 15), ('C#', 1)]

5. lambda and filter() Method to find element in List of tuples


Here, we are using lambda with filter() method to find items in the list of tuples. The filter method takes two arguments a function and iterable. The function runs for each element of iterable and checks which item of iterable returning True or False.

In this code example, the first argument is a function that is lambda in this code and the second argument iterables is list(list_of_tuples)

Code Example

#Program to find an element in a list of tuples Python

list_of_tuples= [("C#", 15), ("C#", 1), ("C++", 3),("C++", 3), ("Python", 25), ("Rust", 30)]



fillter_tuples  =  list(filter(lambda x: x[0] == 'C#', list_of_tuples))


print('found tuples from list of tuples:\n',fillter_tuples)

Output

found tuples from list of tuples:

 [('C#', 15), ('C#', 1)]

6. Using itertools Module


Here, we are using the itertools module takewhile() method to find the element from the list of tuples. The takewhile() allows us to return elements from iterable until the condition is true.

Code Example

#Program to find an element in a list of tuples Python

import itertools
 
list_of_tuples= [("C#", 15), ("C#", 1), ("C++", 3),("C++", 3), ("Python", 25), ("Rust", 30)]



fillter_tuples  =  list(itertools.takewhile(lambda x: x[0]=='C#',list_of_tuples))


print('found tuples from list of tuples:\n',fillter_tuples)

Output

found tuples from list of tuples:

 [('C#', 15), ('C#', 1)]

Conclusion :

We have understood different ways to Find an element in a list of Python tuples. By using all these above ways, we can easily find the element in a list of tuples.