Skip to content
Related Articles

Related Articles

Python | Check if tuple exists as dictionary key

View Discussion
Improve Article
Save Article
  • Last Updated : 26 May, 2022

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 recommended 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




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

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. 

Python3




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

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
Recommended Articles
Page :

Start Your Coding Journey Now!