Sometimes, while working with Python tuples, we can have a problem in which we need to extract all the tuples, which have all the elements in target tuple. This problem can have application in domains such as web development. Let’s discuss certain way in which this problem can be solved.
test_list = [(5, 6, 6), (4, 2, 7), (9, 6, 5, 6)]
test_tuple = (6, 6)
Output : [(5, 6, 6), (9, 6, 5, 6)]
test_list = [(5, 6, 6), (4, 2, 6), (9, 6, 5, 6)]
test_tuple = (6, )
Output : [(5, 6, 6), (4, 2, 6), (9, 6, 5, 6)]
Method : Using
Counter() + list comprehension + all()
The combination of above functions can be used to solve this problem. In this, we perform the task of counting using Counter(). The all(), is used to check if all elements form the subset and list comprehension is used to bind all the logic in one block.
The original list is : [(5, 6, 8), (4, 2, 7), (9, 6, 5, 6)] The superset tuples : [(9, 6, 5, 6)]
- Python - Extract digits from Tuple list
- Python - Extract Kth element of every Nth tuple in List
- Python | Sort tuple list by Nth element of tuple
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python - Extract Item with Maximum Tuple Value
- Python - Extract Even elements in Nested Mixed Tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Extract Dictionary values list to List
- Python | Pair and combine nested list to tuple list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
- Python | Convert mixed data types tuple list to string list
- Python | Convert List of Dictionary to Tuple list
- Python - Convert Tuple value list to List of 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 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.