How to check if key exists in a python dictionary?

In this tutorial, we look at methods to check if a key exists in a dictionary in Python. We also break down the code to facilitate further understanding.

However, in case you are here only for the solution use this link.

Table of Contents - check if key exists in dictionary python

  • Why do we check if key exists in dictionary python?
  • Using the in operator to check if key exists in dictionary python
  • Checking if key exists using the get() method
  • Closing thoughts

If you are here to learn how to check if a key exists in a dictionary in python it is most likely because of these two reasons. Either you are new to python or you tried using the has_key methods and received an error.

The latter is because python3 has removed the has_key methods. However, there are other equally efficient methods to check if a key exists in a dictionary.

Why do we check if a key exists in a python dictionary?

Dictionaries are common and extensively used data types in python. They are used to store key-value pairs and these values are accessed by their respective keys.

This is why it is a good practice to check if the key exists before you try to access its relevant value. Doing so would reduce the likelihood of facing errors.

Now let's look at the different ways you can use to check if a key exists in a dictionary in Python.

Using the in operator to check if key exists in dictionary python:

In this method, we use the membership operator; in. This operator is used to check if one value is a member of another. It returns a boolean value.

In our case, we use the in operator to check if the key is a member of the dictionary.

Code to check if a key exists in dictionary in python:

dict_1 = {"a": 1, "b":2, "c":3}
if "a" in dict_1:
    print("Exists")
else:
    print("Does not exist")

#Output = "Exists"
Now let's check for a negative case:
dict_1 = {"a": 1, "b":2, "c":3}
if "d" in dict_1:
    print("Exists")
else:
    print("Does not exist")

#Output = "Does not exist"

Similarly, the not in operator can also be used to check if a key does not exist in a dictionary.

However, remember the in operator is case sensitive hence you could either ensure all your keys are in the same case or you could use the upper() or lower() methods respectively.

Checking if key exists using the get() method

The get() method is a dictionary method that returns the value of the associated key. If the key is not present it returns either a default value (if passed) or it returns None.

Using this method we can pass a key and check if a key exists in the python dictionary.

Syntax of get()

dict.get(keyname, value)

Here dict is the name of the dictionary you intent to work with

Parameters

Keyname - The keyname of the value to intent to return value - Optional, this value is returned in case the key does not exist

Code to check if the key exists in a dictionary using get()

dict_1 = {"a": 1, "b":2, "c":3}
if dict_1.get("a") is not None:
    print("Exists")
else:
    print("Does not exist")

#Output = "Exists"

And for a negative case,

dict_1 = {"a": 1, "b":2, "c":3}
if dict_1.get("d") is not None:
    print("Exists")
else:
    print("Does not exist")

#Output = "Does not exist"

While using this method keep in mind that this would not be accurate in case you have a key with the value None. If you don't, this method will work fine.

This method returns the values, you could store them in a variable in case you intend to use it.

Closing Thoughts

Although both the aforementioned methods have their limitations, these are more efficient in comparison to the other methods.

Other methods include iterating over the dictionary and then comparing all the keys with a variable containing the key name.

Although all those methods work, they aren't efficient and should only be used to facilitate understanding of the concepts. But in case you are a learner, do try them out.