check if a python dictionary contains a given key

Check If a Python Dictionary Contains a Specific Key

In this tutorial, we will look at how to check whether a dictionary in Python contains a specific key or not.

You can use the membership operator in to check whether a dictionary in Python contains a specific key or not. Alternatively, you can also use the dictionary get() function.

check if a python dictionary contains a given key

Let’s look at some examples for the above methods.

The membership operator is used to test membership in a sequence or collection in Python.

To check if a key exists in a Python dictionary, check for membership of the key in the dictionary using the membership operator in. Here’s an example.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# check if key exists in dictionary
print("Jim" in employees)

Output:

True

We have a dictionary containing names to department mappings of employees in an office. Here, we are checking if the key “Jim” exists in our dictionary employees. We get True as the output because the key “Jim” is present in the dictionary.

Let’s look at another example.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# check if key exists in dictionary
print("Ryan" in employees)

Output:

False

Here we are trying to check whether the key “Ryan” exists in the employee dictionary. We get False as the output because “Ryan” is not present in the dictionary.

The Python dictionary get() function is used to fetch the value corresponding to a key in a dictionary. If the key is not present, it returns None.

Let’s use the get() function to check if the dictionary contains a specific key.

# create a dictionary
employees = {
    "Jim": "Sales",
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# check if key exists in dictionary
print(employees.get("Jim")!=None)

Output:

True

We get True as the output. Same as what we got with the in operator.

Note that there’s a caveat to using this method. What if the key is present but the value corresponding to it is None in the dictionary?

# create a dictionary
employees = {
    "Jim": None,
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# check if key exists in dictionary
print(employees.get("Jim")!=None)

Output:

False

This method doesn’t give the correct result in this case.

Here, we have the key “Jim” present in our dictionary with None as its corresponding value. Since our method compares (using != operator) the return value from the get() function to None we get False as the output.

Alternatively, you can also try to access the value for the key using square brackets [] in a try-except block to check if a key exists in a dictionary.

# create a dictionary
employees = {
    "Jim": None,
    "Dwight": "Sales",
    "Angela": "Accounting"
}
# using try-except
try:
    employees["Jim"]
    print("Key present")
except KeyError:
    print("Key not present")

Output:

Key present

We get the correct result.

In this tutorial, we looked at a few methods to check for the presence of a key in a dictionary. We find that using the membership operator, in is simple and intuitive compared to the other methods.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top