Open In App

Check if Tuple Exists as Dictionary Key – Python

Last Updated : 23 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task is to check if a tuple exists as a key in a dictionary. In Python, dictionaries use hash tables which provide an efficient way to check for the presence of a key. The goal is to verify if a given tuple is present as a key in the dictionary.

For example, given a dictionary d = {(3, 4): ‘gfg’, (5, 6): ‘best’}, we can check if the tuple (3, 4) exists as a key in d. The output will be True since (3, 4) is present as a key in d.

Using in operator

in operator is the most efficient way to check if a key exists in a dictionary in Python. It utilizes the dictionary’s underlying hash table. This method is optimal when we only need to check for the presence of a key and don’t require access to the associated value.

Python
d = { (3, 4): 'gfg', 6: 'is', (9, 1): 'best' }

tup = (3, 5) # target
res = tup in d
print(str(res))

Output
False

Explanation:

  • tup in d checks if the tuple tup exists as a key in the dictionary d.

Using dict.get()

dict.get() method allows us to check if a key exists by attempting to retrieve its associated value. If the key is not found, it returns None . While slightly less efficient than the in operator due to the retrieval of the value, it’s still fast and convenient when we also need to work with the value.

Python
d = { (3, 4): 'gfg', 6: 'is', (9, 1): 'best' }

tup = (3, 5)  # target
res = d.get(tup) is not None
print(str(res)) 

Output
False

Explanation: get() method checks if the key tup exists in the dictionary d. If tup is a key in d, get(t) returns the associated value, otherwise it returns None.

Using dict.keys()

dict.keys() involves explicitly checking if the key exists by iterating over the dictionary’s keys. Although it checks for the presence of the tuple as a key, it is less efficient because it returns a view object that requires iteration over all keys. This makes it slower compared to using in directly.

Python
d = { (3, 4): 'gfg', 6: 'is', (9, 1): 'best' }

tup = (3, 5) # target
res = tup in d.keys()
print(str(res))

Output
False

Explanation: keys() returns a view of all keys in the dictionary and the in operator checks if the tuple tup exists among those keys.

Using try-except block

We can directly attempt to access the key in the dictionary and handle the absence of the key using a try-except block. This approach is generally not preferred for simply checking the existence of a key, as it introduces overhead from exception handling. However, it can be useful if we need to handle the absence of the key in a specific way .

Python
d = { (3, 4): 'gfg', 6: 'is', (9, 1): 'best' }

tup = (3, 5) # target
try:
    res = tup in d
except KeyError:
    res = False
print(str(res))

Output
False

Explanation: try: res = tup in d tries to check if tup exists as a key in the dictionary d using the in operator. If tup exists in d, the result will be True, otherwise it raises a KeyError.



Next Article
Practice Tags :

Similar Reads