Given a dictionary in Python our task is to check whether the given key is already present in the dictionary or not. If present, print “Present” and the value of the key. Otherwise, print “Not present”.
Example
Input : {‘a’: 100, ‘b’:200, ‘c’:300}, key = b
Output : Present, value = 200Input : {‘x’: 25, ‘y’:18, ‘z’:45}, key = w
Output : Not present
There can be different ways for checking if the key already exists, we have covered the following approaches:
- Using the Inbuilt method keys()
- Using if and in
- Using has_key() method
- Using get() method
Check If Key Exists using the Inbuilt method keys()
Using the Inbuilt method keys() method returns a list of all the available keys in the dictionary. With the Inbuilt method keys(), use if statement with ‘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. def checkKey(dic, key): if key in dic.keys(): print ( "Present, " , end = " " ) print ( "value =" , dic[key]) else : print ( "Not present" ) # Driver Code dic = { 'a' : 100 , 'b' : 200 , 'c' : 300 } key = 'b' checkKey(dic, key) key = 'w' checkKey(dic, key) |
Present, value = 200 Not present
Time Complexity: O(n)
Auxiliary Space: O(1)
Check If Key Exists using if and in
This method uses the if statement to check whether the given key exists in the dictionary.
Python3
def checkKey(dic, key): if key in dic: print ( "Present, " , end = " " ) print ( "value =" , dic[key]) else : print ( "Not present" ) # Driver Code dic = { 'a' : 100 , 'b' : 200 , 'c' : 300 } key = 'b' checkKey(dic, key) key = 'w' checkKey(dic, key) |
Present, value = 200 Not present
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.
Check If Key Exists using has_key() method
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
def checkKey(dic, key): if dic.has_key(key): print ( "Present, value =" , dic[key]) else : print ( "Not present" ) # Driver Function dic = { 'a' : 100 , 'b' : 200 , 'c' : 300 } key = 'b' checkKey(dic, key) key = 'w' checkKey(dic, key) |
('Present, value =', 200) Not present
Check If Key Exists using get()
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 is present it will print “Present” Otherwise it will print “Not Present”.
Python3
dic = { 'a' : 100 , 'b' : 200 , 'c' : 300 } # check if "b" is none or not. if dic.get( 'b' ) = = None : print ( "Not Present" ) else : print ( "Present" ) |
Present
Handling ‘KeyError’ Exception
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 1 print ( "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 2 print ( "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" ) |
Example 1 The key doesn't exist in the dictionary Example 2 The key exists in the dictionary
Using count() method
count() method can be used to check if the key exists in the dictionary, if the count of the key is 1 then the key is present else not.
Python3
# Python3 Program to check whether a # given key already exists in a dictionary. # Driver Code dic = { 'a' : 100 , 'b' : 200 , 'c' : 300 } key = 'b' x = list (dic.keys()) res = "Not Present" if (x.count(key) = = 1 ): res = "Present" print (res) |
Present