How to Check if Key Exists in a Python Dictionary?

Python dictionaries are fundamental data structures that contain key-value pairs, where each key corresponds to a value. While using a Dictionary in Python, it is crucial to check whether a specific key exists in a dictionary to avoid errors and improve code efficiency in Python. 

This post aims to provide several methods to check if a key is present/exists in a Python dictionary. 

Let’s take a look at each method individually:

  • Method 1: Using “in” Operator
  • Method 2: Using “get()” Method
  • Method 3: Using “keys()” Method
  • Method 4: Using Exception Handling (Try Except)

Method 1: Using “in” Operator

The below code uses the “in” operator to check if key presents in the given dictionary or not:

Code: 

dict_value = {"Name": "Joseph", "Age": 24, "Height": 5.7}
if "Age" in dict_value:
    print(" Key found in dictionary")

The “in” operator and the if statement is used to determine whether a particular key exists or not in the given dictionary.

Output:

The specific key has been found in the dictionary.

Method 2: Using “get()” Method

The get() method returns the value of an item in a dictionary according to its key. Here is an example:

Code:

dict_value = {"Name": "Joseph", "Age": 24, "Height": 5.7}
if dict_value.get("Height"):
    print(" Key found in dictionary")

The if statement is used along with the “get()” method to check the presence of a key in the given dictionary.

Output:

The specific key has been found in the dictionary.

Method 3: Using “keys()” Method

The keys() method of Python returns a view object containing a dictionary’s keys. Here is an example code:

Code:

dict_value = {"Name": "Joseph", "Age": 24, "Height": 5.7}
if "Age" in dict_value.keys():
    print(" Key found in dictionary")

The if statement is used along with the “keys()” function to check whether the key exists or not in the given dictionary.

Output:

The specific key has been found in the dictionary.

Method 4: Using Exception Handling (Try Except)

Python’s exception handling allows you to handle errors during program execution. The exception handling is used to check if the key exists in the Python dictionary:

Code:

dict_value = {"Name": "Joseph", "Age": 24, "Height": 5.7}
try:
    value = dict_value["Age"]
    print(" Key found in dictionary")
except KeyError:
    print(" Key Not found in dictionary")

A message is printed if a key is present in the dictionary. Its value is assigned to a variable. An error message is printed if the key is not found in the dictionary.

Output:

The key has been found in the dictionary.

Conclusion

The “in” operator, “get() method, “keys()” method, and “exception handling” is used to check if the specified key exists in a Python dictionary. The “in” operator is utilized along with the “if” statement to verify the existence of the key in the dictionary. Similarly, the get() method, keys() method, and other methods can get and check the existence of the keys in a given dictionary. This post presented various ways to check if the key exists/present or not in the dictionary.