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.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
# 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.
# 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.