Sometimes, while working with dictionaries, there is a possibility that it’s keys be in form of tuples. This can be a sub problem to some of web development domain. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using in operator
This is the most recommened and Pythonic way to perform this particular task. It checks for particular tuple and returns True in case of it occurs or False if doesn’t.
# Python3 code to demonstrate working of # Check if tuple exists as dictionary key # using in operator # initialize dictionary test_dict = { (3, 4) : 'gfg', 6 : 'is', (9, 1) : 'best'} # printing original dictionary print("The original dictionary : " + str(test_dict)) # initialize target tuple tar_tup = (3, 4) # Check if tuple exists as dictionary key # using in operator res = tar_tup in test_dict # printing result print("Does tuple exists as dictionary key ? : " + str(res)) |
The original dictionary : {(3, 4): 'gfg', (9, 1): 'best', 6: 'is'}
Does tuple exists as dictionary key ? : True
Method #2 : Using get()
We can use dictionary’s get(), which searches for key in dictionary and if it doesn’t get it, it returns a None. This can be extended in case of tuple keys as well.
# Python3 code to demonstrate working of # Check if tuple exists as dictionary key # using get() # initialize dictionary test_dict = { (3, 4) : 'gfg', 6 : 'is', (9, 1) : 'best'} # printing original dictionary print("The original dictionary : " + str(test_dict)) # initialize target tuple tar_tup = (3, 5) # Check if tuple exists as dictionary key # using get() res = Falseres = test_dict.get(tar_tup) != None # printing result print("Does tuple exists as dictionary key ? : " + str(res)) |
The original dictionary : {(3, 4): 'gfg', (9, 1): 'best', 6: 'is'}
Does tuple exists as dictionary key ? : False
Recommended Posts:
- Python | Test if key exists in tuple keys dictionary
- Python | Check whether given key already exists in a dictionary
- Python | Add dictionary to tuple
- Python | Get all tuple keys from dictionary
- Python | Max/Min of tuple dictionary values
- Python | Tuple key dictionary conversion
- Python - Tuple value product in dictionary
- Python - Summation of tuple dictionary values
- Python | Filter Tuple Dictionary Keys
- Python | Convert list of tuple into dictionary
- Python | Unpacking dictionary keys into tuple
- Python - Dictionary Tuple values update
- Python - Tuple to Dictionary Summation conversion
- Python | Dictionary to list of tuple conversion
- Python - Custom Tuple Key Summation in Dictionary
- Python - Sort dictionary by Tuple Key Product
- Python - Convert Nested Tuple to Custom Key Dictionary
- Python - Remove Disjoint Tuple keys from Dictionary
- Python | Convert tuple to adjacent pair dictionary
- Python | Categorize tuple values into dictionary value 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.

