Check if a tuple exists as dictionary key in Python


A Dictionary is one of the data structures available in python which stores the data in the format of key and value pairs. It is mutable i.e., once the data is defined in the dictionary we can make the modifications on the data. It is an unordered collection of elements. The key in the dictionary is unique and the values can be duplicates. The key and value are separated by using the colon (:).

On the other hand, a tuple is an ordered collection of elements, enclosed in parentheses (), separated by commas. It is immutable, which means the values of the tuple cannot be changed once they are defined. It can contain elements of different types and can be used as keys in dictionaries. The below is the example of the dictionary created by using tuples as the key.

Example

my_dict = {('apple', 'banana'): 1, ('orange', 'grape'): 2} 
print(my_dict)

Output

Following is the output of the above program –

{('apple', 'banana'): 1, ('orange', 'grape'): 2}

There are several approaches available in python for checking if a tuple exists as dictionary key. Let’s go through each approach in detail.

Using the "in" operator

The in operator allows us to check if a key exists in a dictionary. To use it with a tuple, we can enclose the tuple in parentheses and pass it as the key to the dictionary.

Example

In this example, the in operator checks if the given tuple exists as a key in dictionary. If it does, it prints the statement "Tuple exists as a key in the dictionary" otherwise it prints "Tuple does not exist as a key in the dictionary".

my_dict = {('a', 'b'): 42, ('c', 'd'): 99}
my_tuple = ('a', 'b')
if my_tuple in my_dict:
   print("Tuple exists as a key in the dictionary")
else:
   print("Tuple does not exist as a key in the dictionary")

Output

Tuple exists as a key in the dictionary

Using the get() method

The get() method of a dictionary allows us to retrieve the value associated with a given key. By passing a tuple as the key to the get() method, we can check if the tuple exists as a key. If the key does not exist in the dictionary, it returns a default value which is None.

Example

In this example, my_dict.get(my_tuple) retrieves the value associated with my_tuple as the key. If the key exists, the condition my_dict.get(my_tuple) is not None is True, and then it prints the statement "Tuple exists as a key in the dictionary" otherwise it prints "Tuple does not exist as a key in the dictionary".

my_dict = {('a', 'b'): 42, ('c', 'd'): 99}
my_tuple = ('a', 'b')
if my_dict.get(my_tuple) is not None:
   print("Tuple exists as a key in the dictionary")
else:
   print("Tuple does not exist as a key in the dictionary")

Output

Tuple exists as a key in the dictionary

Using exception handling

We can use a try-except block to handle the case where the tuple does not exist as a key in the dictionary. By attempting to access the dictionary using the tuple as the key and catching the KeyError exception, we can determine if the tuple exists or not.

Example

In this example, my_dict[my_tuple] attempts to access the value associated with my_tuple as the key. If the key exists, the value is assigned to the value variable, and then it prints the statement "Tuple exists as a key in the dictionary".

If a KeyError is raised, it means the key does not exist, and then it prints "Tuple does not exist as a key in the dictionary".

my_dict = {'a': 42, ('c', 'd'): 99}
my_tuple = ('a', 'b')
try:
   value = my_dict[my_tuple]
   print("Tuple exists as a key in the dictionary")
except KeyError:
   print("Tuple does not exist as a key in the dictionary")

Output

Tuple does not exist as a key in the dictionary

Updated on: 07-Aug-2023

643 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements