Skip to content
Related Articles

Related Articles

Python | Check if element is present in tuple of tuples

View Discussion
Improve Article
Save Article
  • Last Updated : 19 Sep, 2022
View Discussion
Improve Article
Save Article

Sometimes the data that we use is in the form of tuples and often we need to look into the nested tuples as well. The common problem that this can solve is looking for missing data or na value in data preprocessing. Let’s discuss certain ways in which this can be performed. 

Method #1: Using any() any function is used to perform this task. It just tests one by one if the element is present as the tuple element. If the element is present, true is returned else false is returned. 

Python3




# Python3 code to demonstrate
# test for values in tuple of tuple
# using any()
 
# initializing tuple of tuple
test_tuple = (("geeksforgeeks", "gfg"), ("CS_Portal", "best"))
 
# printing tuple
print("The original tuple is " + str(test_tuple))
 
# using any()
# to test for value in tuple of tuple
if (any('geeksforgeeks' in i for i in test_tuple)):
    print("geeksforgeeks is present")
else:
    print("geeksforgeeks is not present")

Output

The original tuple is (('geeksforgeeks', 'gfg'), ('CS_Portal', 'best'))
geeksforgeeks is present

Method #2: Using itertools.chain() The chain function tests for all the intermediate tuples for the desired values and then returns true if the required value is present in any of the tuples searched. 

Python3




# Python3 code to demonstrate
# test for values in tuple of tuple
# using itertools.chain()
import itertools
 
# initializing tuple of tuple
test_tuple = (("geeksforgeeks", "gfg"), ("CS_Portal", "best"))
 
# printing tuple
print("The original tuple is " + str(test_tuple))
 
# using itertools.chain()
# to test for value in tuple of tuple
if ('geeksforgeeks' in itertools.chain(*test_tuple)):
    print("geeksforgeeks is present")
else:
    print("geeksforgeeks is not present")

Output

The original tuple is (('geeksforgeeks', 'gfg'), ('CS_Portal', 'best'))
geeksforgeeks is present

Method #3: Using list comprehension

Python3




test_tuple = (("geeksforgeeks", "gfg"), ("CS_Portal", "best"))
ele="geeksforgeeks"
x=[ele for i in test_tuple if ele in i]
print(["yes" if x else "no"])

Output

['yes']

Method #4: Using enumerate function

Python3




test_tuple = (("geeksforgeeks", "gfg"), ("CS_Portal", "best"))
ele="geeksforgeeks"
x=[ele for a,i in enumerate(test_tuple) if ele in i]
print(["yes" if x else "no"])

Output

['yes']

Method #5: Using lambda function

Python3




test_tuple = (("geeksforgeeks", "gfg"), ("CS_Portal", "best"))
ele="geeksforgeeks"
x=list(filter(lambda i: (i in test_tuple),test_tuple))
print(["yes" if x else "no"])

Output

['yes']

Method: Using “in” operator 

Python3




test_tuple = (("geeksforgeeks", "gfg"), ("CS_Portal", "best"))
ele="geeksforgeeks" ;a=[]
for i in test_tuple:
  if ele in i:
    a.append(ele)
print(["yes" if a else "no"])

Output

['yes']

Method: Using countOf function 

Python3




import operator as op
test_tuple = (("geeksforgeeks", "gfg"), ("CS_Portal", "best"))
ele="geeksforgeeks" 
x=[j for i in test_tuple for j in i]
print("yes" if op.countOf(x,ele)>0 else "no")

Output

yes

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!