Computer >> Computer tutorials >  >> Programming >> Python

Check if one tuple is subset of other in Python


When it is required to check if one tuple is a subset of the other, the 'issubset' method is used.

The 'issubset' method returns True if all the elements of the set are present in another set, wherein the other set would be passed as an argument to the method.

Otherwise, this method returns False.

Below is a demonstration of the same −

Example

my_tuple_1 = (87, 90, 31, 85)
my_tuple_2 = (34, 56, 12, 5)

print("The first tuple is :")
print(my_tuple_1)
print("The second tuple is :")
print(my_tuple_2)

my_result = set(my_tuple_2).issubset(my_tuple_1)

print("Is the second tuple a subset of the first tuple ? ")
print(my_result)

Output

The first tuple is :
(87, 90, 31, 85)
The second tuple is :
(34, 56, 12, 5)
Is the second tuple a subset of the first tuple ?
False

Explanation

  • Two tuples are defined, and are displayed on the console.
  • The issubset method is used by passing first tuple to it, and comparing it with the second tuple.
  • This result is assigned to a value.
  • It is displayed as output on the console.