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.
# 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 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
Recommended Posts:
- Python | Reverse each tuple in a list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuple from list of tuples if not containing any character
- Python | Swap tuple elements in list of tuples
- Python | Group by matching second tuple value in list of tuples
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Get first element with maximum value in list of tuples
- Python | Accessing nth element from tuples in list
- Python | Preceding element tuples in list
- Python | Get tuple element data types
- Python | Sort a tuple by its float element
- Python | Count occurrences of an element in a Tuple
- Python | Remove particular element from tuple list
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.



