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

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 #2: Using list comprehension. It works only when there are fixed number of elements in every list. For example 2 elements in below code.

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

Method #1 : Using `filter` In this solution, there can be variable number of nodes in lists.

 `# 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``)) ` ` `  `# Prinitng output ` `print``(Output) `

Output:

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

My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.