Check if tuple exists as a dictionary key

Let us consider an e-commerce website that uses a dictionary called ‘products’. This dictionary will have keys representing a tuple of different product ids. Whereas, the values for the corresponding keys can be the category to which the products belong.

Suppose we want to check if a given tuple of product ids exists in the dictionary or not. Therefore, in this tutorial, we will learn to check if a tuple exists as a dictionary key in Python.

Checking if a tuple exists as a dictionary key using the for loop

In the example below, we have taken the test_tuple with product ids (1,2,3). We will check whether the given tuple exists as a key in the products dictionary by applying a for loop over the dictionary. It will iterate over each key present inside the dictionary.

We have also kept a test variable called ‘output’, initialized to False. It will turn ‘True’ when the given tuple is found as a key in the dictionary and we will break out of the for loop. If the output variable’s value is False then it will simply print the corresponding result.

products = { (1,2,3) : 'kitchenware', (4,6) : 'gifts',(7,8) : 'sports items'}

print("The products dictionary : " + str(products))

test_tuple=(1,2,3)

output=False

for key in products:
    if key == test_tuple:
        print(f"The products ids tuple {test_tuple} exists in the dictionary.")
        output=True
        break;
    
if output==False:
    print(f"The products ids tuple {test_tuple} does not exist in the dictionary.")

Output:

The products dictionary : {(1, 2, 3): 'kitchenware', (4, 6): 'gifts', (7, 8): 'sports items'}
The products ids tuple (1, 2, 3) exists in the dictionary.

The products dictionary : {(1, 2, 3): 'kitchenware', (4, 6): 'gifts', (7, 8): 'sports items'}
The products ids tuple (1, 2, 9) does not exist in the dictionary.

Checking if a tuple exists as a dictionary key using the ‘in’ operator

In Python, the ‘in’ operator checks whether a given iterable such as a set, tuple or any individual value is present in another iterable or not. It gives the result as a boolean value in the form of True or False. In the below-mentioned example, we check if the test tuple (4,6) is present in the products dictionary or not. Since it exists in our dictionary we print the corresponding output.

We also check if the tuple (1,6) exists in the products dictionary. Since the output is False, we will print that it does not exists in the dictionary.

products = { (1,2,3) : 'kitchenware', (4,6) : 'gifts', (7,8) : 'sports items'}

print("The products dictionary : " + str(products))

test_tuple=(4,6)

output = test_tuple in products
if output is True:
    print(f"The products ids tuple {test_tuple} exists in the dictionary.")
else:
    print(f"The products ids tuple {test_tuple} does not exist in the dictionary.")

Output:

The products dictionary : {(1, 2, 3): 'kitchenware', (4, 6): 'gifts', (7, 8): 'sports items'}
The products ids tuple (4, 6) exists in the dictionary.

The products dictionary : {(1, 2, 3): 'kitchenware', (4, 6): 'gifts', (7, 8): 'sports items'}
The products ids tuple (1, 6) does not exist in the dictionary.

Checking if a tuple exists as a dictionary key using the ‘get’ operator

The get operator in python will give us the value of the corresponding key in a dictionary in Python. In the below example, we have an output variable that will store the value for the given key inside the parameters of the get method. If the test_tuple does not exist in the dictionary then the get operator will return None. Using this logic, we will test whether a tuple exists as a key in the dictionary by comparing the value of the output variable with None.

products = { (1,2,3) : 'kitchenware', (4,6) : 'gifts',(7,8) : 'sports items'}

print("The products dictionary : " + str(products))

test_tuple=(7,8)

output =  products.get(test_tuple)
if output != None:
    print(f"The products ids tuple {test_tuple} exists in the dictionary.")
else:
    print(f"The products ids tuple {test_tuple} does not exist in the dictionary.")

Output:

The products dictionary : {(1, 2, 3): 'kitchenware', (4, 6): 'gifts', (7, 8): 'sports items'}
The products ids tuple (7, 8) exists in the dictionary.

The products dictionary : {(1, 2, 3): 'kitchenware', (4, 6): 'gifts', (7, 8): 'sports items'}
The products ids tuple (7, 8) exists in the dictionary.

Thus we have reached the end of this tutorial on checking if a tuple exists as a key in a dictionary in Python. To learn more about tuples in Python click on the following link: Tuples in Python with examples

Leave a Reply

Your email address will not be published. Required fields are marked *