Given a list of tuples, the task is to find all those tuples containing the given element, say n.
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.
[(14, 3), (1, 3), (3, 3)]
Method #1 : Using
filter In this solution, there can be variable number of nodes in lists.
[(11, 22), (11, 44), (33, 22, 100, 11), (99, 11)]
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python | Remove tuple from list of tuples if not containing any character
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Convert string tuples to list tuples
- Python | Count the sublists containing given element in a list
- Python | Remove element from given list containing specific digits
- Python | Convert list of tuples to list of list
- Python | How to Concatenate tuples to nested tuples
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Accessing nth element from tuples in list
- Python | Get first element with maximum value in list of tuples
- Python | Preceding element tuples in list
- Python | Filter tuples according to list element presence
- Python - Rear element extraction from list of tuples records
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.