Thursday, December 26, 2024
Google search engine
HomeLanguagesPython | Check if one tuple is subset of other

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 the problem of checking if one subset is part of another. 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(). 

Python3




# 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))


Output : 

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

Time Complexity: O(n) where n is the length of the larger tuple.
Auxiliary Space: O(n) as a new set is created from the tuple to check for the subset.

Method #2: Using all() + generator expression 

The combination of the above functionalities can also perform this task. In this, we check for each element of one tuple with another using expression and all(). 

Python3




# 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))


Output : 

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

Time complexity: O(n*m), where n and m are the lengths of the input tuples. 
Auxiliary space: O(1), as the code uses only a constant amount of extra space to store the variables and the result.

Method #3: Using list comprehension 

Python3




test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 10)
x=[j for i in test_tup1 for j in test_tup2]
print(["yes" if x else "no"])


Output

['yes']

Time complexity: O(m*n) where m and n are the lengths of test_tup1 and test_tup2 respectively.
Auxiliary space: O(m*n) to store the resulting list x.

Method #4: Using in operator

Step-by-step approach:

  • Two tuples test_tup1 and test_tup2 are initialized.
  • The original tuples test_tup1 and test_tup2 are printed.
  • The variable c is initialized to 0 and the variable res is initialized to False.
  • A for loop is used to iterate over each element i in the second tuple test_tup2.
  • Inside the for loop, an if statement is used to check if the element i is present in the first tuple test_tup1.
  • If the element i is present in the first tuple test_tup1, the count variable c is incremented by 1.
  • After the for loop, an if statement is used to check if the count variable c is equal to the length of the second tuple test_tup2.
  • If the count variable c is equal to the length of the second tuple test_tup2, the variable res is set to True.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Check if one tuple is subset of other
 
# 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
c=0
res=False
for i in test_tup2:
    if i in test_tup1:
        c+=1
if(c==len(test_tup2)):
    res=True
         
# printing result
print("Is 2nd tuple subset of 1st ? : " + str(res))


Output

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

Time complexity: O(n), where n is the length of test_tup2.
Auxiliary space: O(1), as only constant extra space is used for the variables c and res.

Method #5: Using enumerate function

Python3




test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 10)
x=[j for a,i in enumerate(test_tup1) for j in test_tup2]
print(["yes" if x else "no"])


Output

['yes']

Time complexity: O(n^2), where n is the length of the longer tuple (test_tup1 in this case).
Auxiliary space: O(n), where n is the length of the longer tuple. 

Method #6: Using set() and <= operator: 

One way to check if one tuple is a subset of another is by converting both tuples to sets and then using the <= operator to check if the first set is a subset of the second.

Python3




# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 10)
 
# check if one tuple is subset of other
res = set(test_tup2) <= set(test_tup1)
 
# printing result
print("Is 2nd tuple subset of 1st ? : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

Is 2nd tuple subset of 1st ? : True

Time complexity: O(n)
Auxiliary Space: O(n)

METHOD 7:Using set difference: The idea is to convert the tuples to sets and find the difference between the first set and the second set. If the length of the difference set is less than the length of the first set, then the second tuple is a subset of the first tuple.

Steps:

  1. Convert both tuples into sets.
  2. Check if the second set is a subset of the first set.
  3. Return True if it is a subset, False otherwise.

Python3




# Python program for the above approach
 
# Function to check if the given sets
# is subset or not
def is_subset(t1, t2):
    set1 = set(t1)
    set2 = set(t2)
    return len(set2.difference(set1)) == 0
   
# Driver Code
t1 = (10, 4, 5, 6)
t2 = (5, 10)
 
print(is_subset(t1, t2))


Output

True

Time Complexity: O(N)
Space Complexity: O(N)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments