In this tutorial, we will look at how to check whether a dictionary in Python contains a specific key or not.
How to check if a key exists in a dictionary?
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.
Let’s look at some examples for the above methods.
Using membership operator, in
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
Using the get()
function
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 –
- Check If a Dictionary Is Empty In Python
- Python Dictionary Pop vs Popitem
- Python Dictionary Items – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.