How to check if a tuple is a subset of another in Python

To check if a tuple is a subset of another in python we’ll have to make 2 tuples, namely tup1 and tup2 which will later check if all the elements of tup2 are present in tup1.

The code for the following program is :

tup1 = ()
tup2 = ()
a=int(input("how many numbers in tuple1 "))
b=int(input("how many numbers in tuple2 "))
for i in range(a):
    s=int(input("enter value"))
    tup1 += (s,)
print(tup1)
for i in range(b):
    s=int(input("enter value"))
    tup2 += (s,)
print(tup2)
counter=0
for i in range(b):
    for j in range(a):
        if tup2[i]==tup1[j]:
            counter += 1
if counter==b:
    print("Yess Tuple2 is a subset of Tuple 1")
else:
    print("Tuple2 is NOT a subset of Tuple 1")

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top