Python | Check if one tuple is subset of other

Sometimes, while working with Python, we can work with different data and we might need to solve problem of checking if one subset is part of other. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using issubset()
We can solve this problem using type conversion of tuple into a set and then check if one tuple is subset of other using issubset().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Check if one tuple is subset of other
# using issubset()
  
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 10)
  
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
  
# Check if one tuple is subset of other
# using issubset()
res = set(test_tup2).issubset(test_tup1)
  
# printing result
print("Is 2nd tuple subset of 1st ? : " + str(res))

chevron_right


Output :

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 10)
Is 2nd tuple subset of 1st ? : True

 

Method #2 : Using all() + generator expression
The combination of above functionalities can also perform this task. In this, we check for each element of one tuple with another using expression and all().



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Check if one tuple is subset of other
# using all() + generator expression
  
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 10)
  
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
  
# Check if one tuple is subset of other
# using all() + generator expression
res = all(ele in test_tup1 for ele in test_tup2)
  
# printing result
print("Is 2nd tuple subset of 1st ? : " + str(res))

chevron_right


Output :

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 10)
Is 2nd tuple subset of 1st ? : True

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :

Be the First to upvote.


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