Python dictionary check if key exists


As a Python developer, you may encounter situations where you need to check if a key exists in a dictionary.

In this article, we will explore 4 different ways to check if a key exists in a Python dictionary.

    Table of Contents

  1. Introduction
  2. Using the get() method
  3. Using the try-except block
  4. Using the setdefault() method
  5. Summary

Understanding Python Dictionaries

Let's have a brieft introduction about dictionary in Python before we move on to the main topic.

Dictionary is a collection of key-value pairs. Each key is associated with a value.

This is used for quick access of data based on a given key.

Here is how a dictionary looks like:

my_dict = {"name": "John", "age": 30, "city": "New York"}

In the above example, we have created a dictionary called my_dict, which has three key-value pairs. The key-value pairs are name: John, age: 30, and city: New York.

Now let's explore different ways to check if a key exists in a Python dictionary.


1. Using the in keyword

The easiest way to check if a key exists in a dictionary is by using the in keyword.

The in keyword returns a Boolean value (True or False) depending on whether the key is present in the dictionary or not.

Example

Checking if "name" exists in the dictionary

my_dict = {"name": "John", "age": 30, "city": "New York"}

if "name" in my_dict:
    print("The key 'name' exists")
else:
    print("The key 'name' does not exist")
The key 'name' exists

In the above example, we are checking if the key "name" exists in the my_dict dictionary using in keyword. Since the key is present in the dictionary, the output will be "The key 'name' exists"


2. Using the get() method

Another way to check if a key exists in a dictionary is by using the get() method.

The get() method returns the value associated with the key if it exists in the dictionary; otherwise, it returns None.

Example

Checking if "name" exists in the dictionary using the get() method

my_dict = {"name": "John", "age": 30, "city": "New York"}

if my_dict.get("name") is not None:
    print("The key 'name' exists")
else:
    print("The key 'name' does not exist")
The key 'name' exists

We used the get() method to check if the key "name" exists in the my_dict dictionary.


3. Using the try-except block

There is another way to check if a key exists in a dictionary. It is by using the try-except block.

The idea here is to try to access the value of the key. If the key exists, the value will be returned. If the key does not exist, an exception will be raised.

Example

Checking if "name" exists in the dictionary using the try-except block

my_dict = {"name": "John", "age": 30, "city": "New York"}

try:
    value = my_dict["name"]
    print("The key 'name' exists")
except KeyError:
    print("The key 'name' does not exist")
The key 'name' exists

In the above example, we are using exception handling to check if the key "name" exists in the `my_dict` dictionary.


4. Using the setdefault() method

The setdefault() method is used to set a default value for a key if it does not exist in the dictionary.

It returns the value associated with the key if it exists in the dictionary; otherwise, it returns the default value.

Using this property we can check if the returned value is the default value or not. If the returned value is the default value, it means that the key does not exist in the dictionary.

Example

Checking if "name" exists in the dictionary using the setdefault() method

my_dict = {"name": "John", "age": 30, "city": "New York"}

value = my_dict.setdefault("name", "Not Found")

if value == "Not Found":
    print("The key 'name' does not exist")
else:
    print("The key 'name' exists")
The key 'name' exists

Summary

In this tutorial, we have learned 4 different ways to check if a key exists in a Python dictionary. You can use any of the methods mentioned above depending on your requirements.

That's all for now. I hope you found this tutorial helpful.

Happy coding! 😇