A dictionary in Python consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.
Input : dict[] = {“neveropen” : 1, “practice” : 2, “contribute” :3} keys[] = {“neveropen”, “practice”} Output : Yes Input : dict[] = {“neveropen” : 1, “practice” : 2, “contribute” :3} keys[] = {“neveropen”, “ide”} Output : No
Let’s discuss various ways of checking multiple keys in a dictionary :
Method #1 Using comparison operator : This is the common method where we make a set which contains keys that use to compare and using comparison operator we check if that key present in our dictionary or not.
Python3
# Python3 code to check multiple key existence # using comparison operator # initializing a dictionary sports = {"neveropen" : 1 , "practice" : 2 , "contribute" : 3 } # using comparison operator print (sports.keys() > = {"neveropen", "practice"}) print (sports.keys() > = {"contribute", "ide"}) |
True False
Time complexity: O(k), where k is the number of keys in the dictionary
Auxiliary space: O(k), to create the set of keys for comparison.
Method #2 Using issubset() : In this method, we will check the keys that we have to compare is subset() of keys in our dictionary or not.
Python3
# Python3 code heck multiple key existence # using issubset # initializing a dictionary sports = {"neveropen" : 1 , "practice" : 2 , "contribute" : 3 } # creating set of keys that we want to compare s1 = set ([ 'neveropen' , 'practice' ]) s2 = set ([ 'neveropen' , 'ide' ]) print (s1.issubset(sports.keys())) print (s2.issubset(sports.keys())) |
True False
Time complexity: O(n), where n is the length of the test_keys list.
Auxiliary space: O(n), as we are creating a dictionary with n key-value pairs.
Method #3 Using if and all statement : In this method we will check that if all the key elements that we want to compare are present in our dictionary or not .
Python3
# Python3 code check multiple key existence # using if and all # initializing a dictionary sports = {"neveropen" : 1 , "practice" : 2 , "contribute" : 3 } # using if, all statement if all (key in sports for key in ( 'neveropen' , 'practice' )): print ("keys are present") else : print ("keys are not present") # using if, all statement if all (key in sports for key in ( 'neveropen' , 'ide' )): print ("keys are present") else : print ("keys are not present") |
keys are present keys are 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.
Method #4 : Alternatively, you can use a list comprehension and list comparison to achieve a similar result:
Python3
# initializing a dictionary sports = { "neveropen" : 1 , "practice" : 2 , "contribute" : 3 } # using a list comprehension to check multiple key existence keys_to_check1 = [ 'neveropen' , 'practice' ] keys_to_check2 = [ 'neveropen' , 'ide' ] keys_present1 = [key for key in keys_to_check1 if key in sports] keys_present2 = [key for key in keys_to_check2 if key in sports] print (keys_present1 = = keys_to_check1) print (keys_present2 = = keys_to_check2) # This code is contributed by Edula Vinay Kumar Reddy |
True False
Time complexity: O(n),
Auxiliary Space: O(n) and, since it involves a single pass through the list of keys to check. It uses a list comprehension to generate a list of keys that are present in the dictionary.