5 Ways to check key exists in Python dictionary

Python Dictionary

In this post, we are going to learn 5 Ways to check key exists in Python dictionary. We access the value from the dictionary using a key, but sometimes the key does not exist in the dictionary what we are looking for so it throws an error. It is good practice to check if the key exists in a dictionary or not before doing any operations.

Important Note: Till python 2.7 we were using dict.has_key(key) to check the key that exists in the dictionary. But it is depreciated in python 3.

5 Ways to check key exists in Python dictionary

1. keys() to check key exists in Python dictionary


The keys() dictionary method returns a sequence of all dictionary keys(). If we have to find out if any specific key exists in the dictionary then we can use it with In operator.

In this code example, we are using keys() for all dictionary keys, and checking the ‘Phy’ key exists in a dictionary by using In operator with if statement. If it returns true then ‘Phy key exists in the dictionary’ will be the output.

Python Program Example

dict_sub = {'math':100,'Eng':100,'Chem':98,'Phy':100,'Sci':100}
def key_exist(dict,key):
 if key in dict.keys():
  print(key,'is exists in the dictionary')
 else:
  print(key,' does not exist in Dictionary')

#calling key_exist function
key_exist(dict_sub,'Phy')

Output

Phy key is exists in the dictionary

2. get() to check key exists in Python Dictionary


get() dictionary method takes two arguments, a key that we are looking at in dictionary and default values as an argument. If the key exists in the dictionary then the value of the key is returned else default value return is none.

Syntax

dict.get(key,defultvalue)

Python Program Example

dict_sub = {'math':100,'Eng':100,'Chem':98,'Phy':100,'Sci':100}
def key_exist(dict,key): 
 if dict.get(key)is not None:
  print(key,'key is exist in the dictionarty')
 else:
  print(key,'key does not exist in Dictionary')

#calling key_exist function
key_exist(dict_sub,'Phy')

Output

Phy key is exist in the dictionary

3. IN operator to check key exists in Python Dictionary


We can use the ‘in’ operator with the if statement to check if the key exists in Dictionary. If a key exists in the dictionary it returns true else return false.

In this code example, we are checking for the key ‘Phy’ to exist. The key ‘Phy’ exists in the dictionary so it is returning true and print ‘PHY ,key exists in the dictionary

Python Program Example

dict_sub = {'math':100,'Eng':100,'Chem':98,'Phy':100,'Sci':100}
def key_exist(dict,key): 
 if key in dict:
  print(key,'key is exist in the dictionary')
 else:
  print(key,'key does not exist in Dictionary')

#calling key_exist function
key_exist(dict_sub,'Phy')

Output

Phy key is exist in the dictionary

4. try/Except to check key exists in Python Dictionary


Try/except are used for exception handing in python the same as in other languages. But in Python, we can use these statements to check if the key exists in the dictionary.

In this code example, we are checking for the key ‘music’, it does not exist in the dictionary so the output showing that this key does not exist in the dictionary.

Python Program Example

dict_sub = {'math':100,'Eng':100,'Chem':98,'Phy':100,'Sci':100}
def key_exist(dict,key): 
 try:
   dict_sub[key]
   return True
 except KeyError:
     print('key does not exist in Dictionary')
  
 

#calling key_exist function
key_exist(dict_sub,'music')

Output

key does not exist in Dictionary

5. Not and IN operators to check key exists in Dictionary


We can use the not operator with IN operator to check the key that exists in the dictionary. If it returns true then the key does not exist in the dictionary.

In this code example, we are checking if the key ‘music’ does not exist in the dictionary and it returns true.

Python Program Example

dict_sub = {'math':100,'Eng':100,'Chem':98,'Phy':100,'Sci':100}
def key_exist(dict,key): 
 if key not in dict_sub:
   print('Key does not exist dictionary')
 else:
     print('key exist in Dictionary')
  
 
#calling key_exist function
key_exist(dict_sub,'music')

Output

Key does not exist dictionary

Conclusion:

In this post, we have learned different 5 Ways to check key exists in Python dictionary. We hope these techniques will help you to check if a key exists in Dictionary.

Happy Learning!!