Check whether given Key already exists in a Python Dictionary

01 Jul, 2022
In this article, we will cover checking if the list of keys exists in the dictionary in Python. 
In this, we will try to cover different methods to identify whether the key is present in the dictionary or not. Here is a short introduction to what we will cover in this section:
Using the Inbuilt method keys() 
Using if and in
Using has_key() method
Using has_key() method
Given a dictionary in Python, write a Python program to check whether a given key already exists in a dictionary. If present, print “Present” and the value of the key. Otherwise, print “Not present”. 
Examples:
Input : {'a': 100, 'b':200, 'c':300}, key = b
Output : Present, value = 200

Input : {'x': 25, 'y':18, 'z':45}, key = w
Output : Not present
Using the Inbuilt method keys() method returns a list of all the available keys in the dictionary. With the Inbuilt method keys(), use the if statement and the ‘in’ operator to check if the key is present in the dictionary or not. 
Python3
# Python3 Program to check whether a# given key already exists in a dictionary. # Function to print sumdef checkKey(dict, key):         if key in dict.keys():        print("Present, ", end =" ")        print("value =", dict[key])    else:        print("Not present") # Driver Codedict = {'a': 100, 'b':200, 'c':300} key = 'b'checkKey(dict, key) key = 'w'checkKey(dict, key)
Output:
Present, value = 200
Not present
This method simply uses the if statement to check whether the given key exists in the dictionary. 
Python3
# Python3 Program to check whether a# given key already exists in a dictionary. # Function to print sumdef checkKey(dict, key):         if key in dict:        print("Present, ", end =" ")        print("value =", dict[key])    else:        print("Not present") # Driver Codedict = {'a': 100, 'b':200, 'c':300} key = 'b'checkKey(dict, key) key = 'w'checkKey(dict, key)
Output:
Present, value = 200
Not present
Using has_key() method returns true if a given key is available in the dictionary, otherwise, it returns a false. With the Inbuilt method has_key(), use the if statement to check if the key is present in the dictionary or not. Note – has_keys() method has been removed from the Python3 version. Therefore, it can be used in Python2 only. 
Python
# Python3 Program to check whether a# given key already exists in a dictionary. # Function to print sumdef checkKey(dict, key):         if dict.has_key(key):        print "Present, value =", dict[key]    else:        print "Not present" # Driver Functiondict = {'a': 100, 'b':200, 'c':300}key = 'b'checkKey(dict, key) key = 'w'checkKey(dict, key)
Output:
Present, value = 200
Not present
Using the Inbuilt method get() method returns a list of available keys in the dictionary. With the Inbuilt method keys(), use the if statement to check if the key is present in the dictionary or not. If the key will present it will print “Present” Otherwise it will print “Not Present”.
Python3
dict = {'a': 100, 'b':200, 'c':300} # cheack if "b" is none or not.if dict.get('b') == None:   print("Not Present") else:   print("Present")
Output:
Present
Use try and except to handle the KeyError exception to determine if a key is present in a dict. The KeyError exception is generated if the key you’re attempting to access is not present in the dictionary.
Python3
dictExample = {'Aman': 110, 'Rajesh': 440, 'Suraj': 990} # Example 1print("Example 1") try:     dictExample["Kamal"]     print('The key exists in the dictionary') except KeyError as error:     print("The key doesn't exist in the dictionary")  # Example 2print("Example 2") try:     dictExample["Suraj"]     print('The key exists in the dictionary') except KeyError as error:     print("The given key doesn't exist in the dictionary")
Output:
Example 1
The key doesn't exist in the dictionary
Example 2
The key exists in the dictionary
surajkumarguptaintern
Python dictionary-programs
python-dict
Python
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Iterate over a list in Python
Introduction To PYTHON