Given a list of tuples, the task is to find all those tuples containing the given element, say n.
Examples:
Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)]
Output: [(11, 22), (11, 44)]Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)]
Output: [(14, 3), (1, 3), (3, 3)]
There are multiple ways we can find the tuples containing the given element from a list of tuples. Let’s see some of Pythonic ways to do this task.
Method #1: Using for loop
Approach:
- Define a list of tuples named Input.
- Assign the value 11 to a variable x.
- Define an empty list Output.
- Iterate through each tuple in the Input list using a for loop.
- Within the loop, use the in operator to check if the value of x is present in the current tuple.
- If the value of x is present in the current tuple, append the tuple to the Output list using the append() method.
- Once the loop is finished, print the Output list using the print() function.
Python3
# Python code to find the tuples containing # the given element from a list of tuples # List of tuples Input = [( 11 , 22 ), ( 33 , 55 ), ( 55 , 77 ), ( 11 , 44 ), ( 33 , 22 , 100 , 11 ), ( 99 , 11 )] x = 11 Output = [] for i in Input : if x in i: Output.append(i) # Printing output print (Output) |
[(11, 22), (11, 44), (33, 22, 100, 11), (99, 11)]
Time complexity: O(n), where n is the number of tuples in the list, because it has to traverse the list once to find the tuples that contain the given element.
Auxiliary space: O(m), where m is the number of tuples that contain the given element, because it needs to store all the tuples that contain the element in a separate list.
Method #2: Using list comprehension.
It works only when there is a fixed number of elements in every list. For example 2 elements in the below code.
Python3
# Python code to find the tuples containing # the given element from a list of tuples # List of tuples Input = [( 14 , 3 ),( 23 , 41 ),( 33 , 62 ),( 1 , 3 ),( 3 , 3 )] # Find an element in list of tuples. Output = [item for item in Input if item[ 0 ] = = 3 or item[ 1 ] = = 3 ] # printing output print (Output) |
[(14, 3), (1, 3), (3, 3)]
Time complexity: O(n), where n is the number of tuples in the input list. The reason is that the code is iterating through the input list once and checking each tuple.
Auxiliary space: O(m), where m is the number of tuples that match the given condition (element equal to 3).
Method #3: Using filter In this solution, there can be a variable number of nodes in lists.
Python3
# Python code to find the tuples containing # the given element from a list of tuples # List of tuples Input = [( 11 , 22 ), ( 33 , 55 ), ( 55 , 77 ), ( 11 , 44 ), ( 33 , 22 , 100 , 11 ), ( 99 , 11 )] # Using filter Output = list ( filter ( lambda x: 11 in x, Input )) # Printing output print (Output) |
[(11, 22), (11, 44), (33, 22, 100, 11), (99, 11)]
Time complexity: O(n), where n is the number of tuples in the list. This is because the filter function needs to iterate over all the tuples to find the ones containing the given element.
Auxiliary space: O(m), where m is the number of tuples containing the given element. This is because the filter function creates a new list with the filtered tuples. The size of this list will depend on the number of tuples that contain the given element.
Method #4: Using any()
Approach is using any function and list comprehension.
Python3
Input = [( 11 , 22 ), ( 33 , 55 ), ( 55 , 77 ), ( 11 , 44 )] x = 11 Output = [ tuple for tuple in Input if any (x = = i for i in tuple )] print (Output) #This code is contributed by Edula Vinay Kumar Reddy |
[(11, 22), (11, 44)]
Time complexity: O(n), where n is the number of elements in the input list. This is because the list comprehension iterates through each tuple in the input list and any function iterates through each element in the tuple.
Auxiliary space: (n), as the output list, will have a maximum size of n if every tuple in the input list contains the element x.
Method#5:Using reduce() + filter() +lambda() function.
Python3
from functools import reduce #Initializing tuples in a list Input = [( 11 , 22 ), ( 33 , 55 ), ( 55 , 77 ), ( 11 , 44 )] #value needs to search x = 11 #using reduce() function along with filter() function Output = list ( filter ( lambda i: reduce ( lambda x, y: x or y, map ( lambda z: z = = x, i)), Input )) #printing output print (Output) |
[(11, 22), (11, 44)]
Time complexity: O(n*m), where n is the length of the input list and m is the maximum length of a tuple in the list
Auxiliary Space: O(1)
Method #6 : Using count() method
Python3
# Python code to find the tuples containing # the given element from a list of tuples # List of tuples Input = [( 11 , 22 ), ( 33 , 55 ), ( 55 , 77 ), ( 11 , 44 ), ( 33 , 22 , 100 , 11 ), ( 99 , 11 )] x = 11 Output = [] for i in Input : if i.count(x)> = 1 : Output.append(i) # Printing output print (Output) |
[(11, 22), (11, 44), (33, 22, 100, 11), (99, 11)]
Time Complexity: O(n), where n is the number of tuples in the input list.
Auxiliary Space: O(k), where k is the number of tuples in the output list.
Method #7: Using itertools
Approach:
- Importing the itertools module.
- Defining a list of tuples called “Input“.
- Defining a variable “x” with a value of 11.
- Using list comprehension and the built-in “in” operator to create a new list of tuples called “Output“, containing only the tuples from “Input” that contain the value of “x” using the “if” statement.
- Using the itertools.chain() function to chain the elements of the “Output” list into a single list.
- Printing the final output.
Below is the implementation of the above approach:
Python3
import itertools # List of tuples Input = [( 11 , 22 ), ( 33 , 55 ), ( 55 , 77 ), ( 11 , 44 ), ( 33 , 22 , 100 , 11 ), ( 99 , 11 )] x = 11 Output = list (itertools.chain([i for i in Input if x in i])) # Printing output print (Output) #This code is contributed by Vinay Pinjala. |
[(11, 22), (11, 44), (33, 22, 100, 11), (99, 11)]
Time Complexity:O(N)
Auxiliary Space :O(N)
Method #8: Using list comprehension with conditional expression
Use list comprehension to create a new list containing only the tuples from the input list that contain the element x. The conditional expression if x in i filters out the tuples that don’t contain x. Finally, the resulting list is assigned to the variable Output and printed to the console.
Python3
# Python code to find the tuples containing # the given element from a list of tuples # List of tuples Input = [( 11 , 22 ), ( 33 , 55 ), ( 55 , 77 ), ( 11 , 44 ), ( 33 , 22 , 100 , 11 ), ( 99 , 11 )] # Element to search for x = 11 # Using list comprehension with conditional expression # to create a list of tuples containing x Output = [i for i in Input if x in i] # Printing output print (Output) |
[(11, 22), (11, 44), (33, 22, 100, 11), (99, 11)]
Time Complexity: O(N)
Auxiliary Space: O(N)