A data structure that is known as an associative array in Python is called a dictionary. A dictionary contains a collection of key-value pairs. It provides a mapping of the key-value pair to its value.
We can check if the key exists in Python dictionary with the help of various ways using built-in functions in Python.
This article will discuss six different ways to check if a key exists in Python Dictionary. There will be example code snippets along with output showing if a key exists or not.
Table of Contents
1. Python keys( ) Method:
Python keys( ) method is used to get a list of all the keys of dictionary elements. It is a built-in function. The ‘in’ operator is used along with this method to check if the key is present or not.
dictionary = {'New York': "2", 'Chicago': "4", 'Houston': "6", 'Washington':"8"}
key = 'Houston'
if key in dictionary.keys():
print( "Yes, this Key is Present" )
else:
print( "No, this Key does not exist in Dictionary" )
Output:

2. Python 'if' and 'in' Statement:
We can use the conditional statement ‘if ‘and ‘in‘ operator to check a key in the dictionary list.
dictionary = {'New York': "2", 'Chicago': "4", 'Houston': "6", 'Washington': "8"}
key = 'Los Angeles'
if key in dictionary.keys():
print("Yes, this Key is Present")
else:
print("No, this Key does not exist in Dictionary")
Output:

3. Python 'if not in' Statement.
There is a way apart from checking the availability of keys in a dictionary. We can check if the key doesn’t exist by using the ‘not in‘ statement. The ‘not in‘ statement will return True if the key is not there.
dictionary = {'New York': "2", 'Chicago': "4", 'Houston': "6", 'Washington': "8"}
key = 'San Francisco'
if key not in dictionary:
print("No, this Key does not exist in the dictionary.")
else:
print("Yes, this Key is Present")
Output:

4. Python get( ) function
get( ) is a Python built-in function. If the dictionary key is present, this method returns the value associated with the key according to key-value pairs. Whereas it returns none when there is no key.
dictionary = {'New York': "2", 'Chicago': "4", 'Houston': "6", 'Washington': "8"}
if dictionary.get('Chicago')!=None:
print("Yes, this Key is Present")
else:
print("No, this Key does not exist in the dictionary.")
Output:

5. Python try/except
We can get this job done by using try/except logic. When a key is not present in the dictionary, and we try to access it, it returns a keyError. In this way, we can check the presence of a key in the dictionary.
def key_check(dict_test, key):
try:
value = dict_test[key]
return True
except KeyError:
return False
dictionary = {'New York': "2", 'Chicago':"4", 'Houston':"6", 'Washington':"8"}
key = 'New York'
if key_check(dictionary, key):
print("Yes, this Key is Present")
else:
print("No, this Key does not exist in the dictionary.")
Output:

CONCLUSION:
Different techniques are explained along with examples to understand how to check a key in if it is already present in a Python Dictionary or not. I hope this article will be helpful in your development practice.