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 | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Convert string tuples to list tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python | Get first element with maximum value in list of tuples
- Python | Accessing nth element from tuples in list
- Python | Preceding element tuples in list
- Python | Filter tuples according to list element presence
- Python - Rear element extraction from list of tuples records
- Python | Find overlapping tuples from list
- Python | Find top K frequent elements from a list of tuples
- Python - Find the Maximum of Similar Indices in two list of Tuples
- Python | How to Concatenate tuples to nested tuples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.