How to check if Dictionary has key in Python

Python dictionary is one of the most used data structures, and it is used to store the data in key-value pairs. To access the data of the dictionary, you need a key. In some cases, we try to access a key that does not exist in the dictionary that’s why it is a fair practice to check if the key exists before you attempt to access its value.

There are different ways to check whether the dictionary has a key or a key exists in the dictionary. Let’s check out one by one.

Python check if dictionary has key

To check if a dictionary has a key in Python, use the “in” membership operator. The “in” operator is used to check if a value is an element of an iterator, and if it does, it returns True otherwise, it returns a False.

The in operator in Python returns a boolean value and is easily one of the most used operators when you want to check whether the value exists in the iterator or not.

Code

Python code to check if a dictionary has a key:

dict = {
 "event": "New Year",
 "year": "2022",
 "location": "Rajkot",
}

if "year" in dict:
 print("The dictionary has a key")
else:
 print("The key does not exist")

Output

The dictionary has a key

In this example, first, we defined a dictionary using { } (curly braces). The dictionary has three key-value pairs.

In the next step, if used if statement with in operator to check whether the given key exists in the dictionary. If it does, then if statement will be executed otherwise, else.

The year key exists in the dictionary. That’s why if statement is executed and prints “The dictionary has a key”.

Let’s take an example where the key does not exist.

dict = {
 "event": "New Year",
 "year": "2022",
 "location": "Rajkot",
}

if "status" in dict:
 print("The dictionary has a key")
else:
 print("The key does not exist")

Output

The key does not exist

You can see that the “status” key is not there in the dictionary. That’s why it prints “The key does not exist”.

One thing to note here is that the in operator is case-sensitive. Therefore, you could either confirm all your keys are in the same case, whether it is lower or upper case and to do that, you can use either upper() or lower() methods, respectively.

By using not in operator to check if dict has key

The “not in” operator works exactly the opposite of the “in” operator. The “not in” operator checks if a key does not exist in a dictionary.

Code

Python code to check if a dictionary has not a key using not in operator:

dict = {
 "event": "New Year",
 "year": "2022",
 "location": "Rajkot",
}

if "status" not in dict:
 print("Yes, the key does not exist in the dictionary")
else:
 print("The dictionary has a key")

Output

The key does not exist in the dictionary

The status does not exist in the dictionary, and we confirm it by using not in operator that’s why if condition returns True and prints the output.

By using dict.get() method

The dict.get() is a built-in Python method that returns the value of the associated key. If the key is not present in the dictionary then it returns either a default value or None.

Syntax of get()

dict.get(keyname, value)

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

dict = {
 "event": "New Year",
 "year": "2022",
 "location": "Rajkot",
}

if dict.get("event") is not None:
 print("The dict has a key")
else:
 print("The dict has not a key")

Output

The dict has a key

In some instances, dict.get() method can give you the accurate result but in some cases, this approach is not suitable. For example, if you have a key with the value None then this approach will not work.

Summary

Using “in operator” or “not in operator”, you can check whether the dictionary has a key or not in Python. This is the best suitable approach for your requirements. That’s it for this tutorial.

More posts

Python null coalesce

Python **kwargs get

Python elapsed time

Leave a Comment