Open in App
Not now

# Python | Find the tuples containing the given element from a list of tuples

• Difficulty Level : Basic
• Last Updated : 03 Feb, 2023

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

## 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)`

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)`

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)`

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 the any function and a 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`

Output

`[(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 the 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)`

Output

`[(11, 22), (11, 44)]`

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)`

Output

`[(11, 22), (11, 44), (33, 22, 100, 11), (99, 11)]`

Time Complexity : O(N)
Auxiliary Space : O(N)

My Personal Notes arrow_drop_up
Related Articles