Open In App

Python – Test if Tuple contains K

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python, we can have a problem in which we have a record and we need to check if it contains K. This kind of problem is common in data preprocessing steps. Let’s discuss certain ways in which this task can be performed.

Method #1: Using any() + map() + lambda Combination of above functions can be used to perform this task. In this, we check for any element using any() and extension of logic is done by map() and lambda. 

Python3




# Python3 code to demonstrate working of
# Test if Tuple contains K
# using any() + map() + lambda
 
# initialize tuple
test_tup = (10, 4, 5, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# initialize K
K = 6
 
# Test if Tuple contains K
# using any() + map() + lambda
res = any(map(lambda ele: ele is K, test_tup))
 
# printing result
print("Does tuple contain any K value ? : " + str(res))


Output : 

The original tuple : (10, 4, 5, 6, 8)
Does tuple contain any K value ? : True

Method #2: Using loop This task can be performed using loop as well using brute force constructs. We just iterate through the tuple and when we encounter K, we set flag to True and break. 

Python3




# Python3 code to demonstrate working of
# Test if Tuple contains K
# Using loop
 
# initialize tuple
test_tup = (10, 4, 5, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# initialize K
K = 6
 
# Test if Tuple contains K
# Using loop
res = False
for ele in test_tup:
    if ele == K:
        res = True
        break
 
# printing result
print("Does tuple contain any K value ? : " + str(res))


Output : 

The original tuple : (10, 4, 5, 6, 8)
Does tuple contain any K value ? : True

Method #3: Using in operator

Python3




# Python3 code to demonstrate working of
# Test if Tuple contains K
 
# Initialize tuple
test_tup = (10, 4, 5, 6, 8)
 
# Printing original tuple
print("The original tuple : " + str(test_tup))
 
# Initialize K
K = 6
 
# Test if Tuple contains K
res=False
if K in test_tup:
    res=True
# Printing result
print("Does tuple contain any K value ? : " + str(res))


Output

The original tuple : (10, 4, 5, 6, 8)
Does tuple contain any K value ? : True

Method #4 : Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Test if Tuple contains K
import operator as op
# Initialize tuple
test_tup = (10, 4, 5, 6, 8)
 
# Printing original tuple
print("The original tuple : " + str(test_tup))
 
# Initialize K
K = 6
res = op.countOf(test_tup, K) > 0
# Printing result
print("Does tuple contain any K value ? : " + str(res))


Output

The original tuple : (10, 4, 5, 6, 8)
Does tuple contain any K value ? : True

Time Complexity: O(n)
Auxiliary Space: O(1)

Method #5: Using set()

Python3




#Python3 code to demonstrate working of
#Test if Tuple contains K
#Initialize tuple
test_tup = (10, 4, 5, 6, 8)
 
#Printing original tuple
print("The original tuple : " + str(test_tup))
 
#Initialize K
K = 6
 
#Convert tuple to set
test_set = set(test_tup)
 
#Test if set contains K
res = K in test_set
 
#Printing result
print("Does tuple contain any K value ? : " + str(res))


Output

The original tuple : (10, 4, 5, 6, 8)
Does tuple contain any K value ? : True

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

Method #6: Using the set intersections() function and bool() function 

Step-by-step approach:

  • Initialize the tuple to be checked.
  • Print the original tuple.
  • Initialize the value of K that needs to be checked in the tuple.
  • Check if the tuple contains the value K using set() and intersection().
  • Convert the tuple to a set using the set() method.
  • Create a set with the value K and use the intersection() method to check if there is any common element in the two sets.
  • If the intersection contains any element, then the tuple contains the value K.
  • Print the result whether the tuple contains K or not.

Python3




# Python3 code to demonstrate working of
# Test if Tuple contains K
# using set() and intersection()
 
# initialize tuple
test_tup = (10, 4, 5, 6, 8)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# initialize K
K = 6
 
# Test if Tuple contains K
# using set() and intersection()
res = bool(set(test_tup).intersection(set([K])))
 
# printing result
print("Does tuple contain any K value ? : " + str(res))


Output

The original tuple : (10, 4, 5, 6, 8)
Does tuple contain any K value ? : True

Time Complexity: The time complexity of this approach is O(n) since we need to check all the elements of the tuple once.
Auxiliary Space: The space complexity of this approach is O(n) since we need to create a set of size n to store the elements of the tuple.

Method #7: Using index() method

  • Initialize the tuple and K value as given in the problem statement.
  • Use the index() method to check if the K value is present in the tuple.
  • If the K value is present in the tuple, index() method will return its index position, which is greater than or equal to 0.
  • If the K value is not present in the tuple, index() method will raise a ValueError exception.
  • Catch the exception using a try-except block and return False.
  • If no exception is raised, return True.

Python3




# Python3 code to demonstrate working of
# Test if Tuple contains K
# Initialize tuple
test_tup = (10, 4, 5, 6, 8)
 
# Printing original tuple
print("The original tuple : " + str(test_tup))
 
# Initialize K
K = 6
 
# Check if tuple contains K using index() method
try:
    test_tup.index(K)
    res = True
except ValueError:
    res = False
 
# Printing result
print("Does tuple contain any K value ? : " + str(res))


Output

The original tuple : (10, 4, 5, 6, 8)
Does tuple contain any K value ? : True

Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(1), as we are not using any extra data structure.



Last Updated : 15 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads