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.
Input :
test_list = [(5, 6, 6), (4, 2, 7), (9, 6, 5, 6)]
test_tuple = (6, 6)
Output : [(5, 6, 6), (9, 6, 5, 6)]Input :
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.
# Python3 code to demonstrate working of # Extract tuple supersets from List# Using all() + list comprehension + Counterfrom collections import Counter # initializing listtest_list = [(5, 6, 8), (4, 2, 7), (9, 6, 5, 6)] # printing original listprint("The original list is : " + str(test_list)) # initializing tupletest_tup = (6, 6, 5) # Extract tuple supersets from List# Using all() + list comprehension + Counterres = [sub for sub in test_list if all(Counter(sub)[x] >= Counter(test_tup)[x] for x in Counter(test_tup))] # printing result print("The superset tuples : " + str(res)) |
The original list is : [(5, 6, 8), (4, 2, 7), (9, 6, 5, 6)] The superset tuples : [(9, 6, 5, 6)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


