Python | Check if tuple exists as dictionary key

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :



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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 = False
res = test_dict.get(tar_tup) != None 
  
# printing result
print("Does tuple exists as dictionary key ? : " + str(res))

chevron_right


Output :

The original dictionary : {(3, 4): 'gfg', (9, 1): 'best', 6: 'is'}
Does tuple exists as dictionary key ? : False



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.