Python | Check if element is present in tuple of tuples

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 a missing data or N.A values 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.