Check if one tuple is subset of other in Python

PythonServer Side ProgrammingProgramming

Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

Practical Data Science using Python

22 Lectures 6 hours

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

Live Demo

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.
Updated on 11-Mar-2021 09:54:09