Sometimes, while working with Python dictionaries, we can have a problem in which we need to compare dictionaries for equality on bases in selected keys. This kind of problem is common and has application in many domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop This is brute force way in which this task can be performed. In this, we iterate for both the dictionary and manually test for keys equality using equality operator from selected keys.
Python3
# Python3 code to demonstrate working of # Compare Dictionaries on certain Keys # Using loop # initializing dictionaries test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'Lazyroar' : 5 } test_dict2 = { 'gfg' : 2 , 'is' : 3 , 'best' : 3 , 'for' : 7 , 'Lazyroar' : 5 } # printing original dictionaries print ("The original dictionary 1 : " + str (test_dict1)) print ("The original dictionary 2 : " + str (test_dict2)) # initializing compare keys comp_keys = [ 'best' , 'Lazyroar' ] # Compare Dictionaries on certain Keys # Using loop res = True for key in comp_keys: if test_dict1.get(key) ! = test_dict2.get(key): res = False break # printing result print ("Are dictionary equal : " + str (res)) |
The original dictionary 1 : {‘Lazyroar’: 5, ‘gfg’: 1, ‘is’: 2, ‘for’: 4, ‘best’: 3} The original dictionary 2 : {‘Lazyroar’: 5, ‘gfg’: 2, ‘is’: 3, ‘for’: 7, ‘best’: 3} Are dictionary equal : True
Method #2 : Using all() This is one liner alternative to perform this task. In this the functionality of comparison is done using all(), comparing all required keys.
Python3
# Python3 code to demonstrate working of # Compare Dictionaries on certain Keys # Using all() # initializing dictionaries test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'Lazyroar' : 5 } test_dict2 = { 'gfg' : 2 , 'is' : 3 , 'best' : 3 , 'for' : 7 , 'Lazyroar' : 5 } # printing original dictionaries print ("The original dictionary 1 : " + str (test_dict1)) print ("The original dictionary 2 : " + str (test_dict2)) # initializing compare keys comp_keys = [ 'best' , 'Lazyroar' ] # Compare Dictionaries on certain Keys # Using all() res = all (test_dict1.get(key) = = test_dict2.get(key) for key in comp_keys) # printing result print ("Are dictionary equal : " + str (res)) |
The original dictionary 1 : {‘Lazyroar’: 5, ‘gfg’: 1, ‘is’: 2, ‘for’: 4, ‘best’: 3} The original dictionary 2 : {‘Lazyroar’: 5, ‘gfg’: 2, ‘is’: 3, ‘for’: 7, ‘best’: 3} Are dictionary equal : True
Method #3 : Using Dictionary comprehension and all() function
Approach
we use dictionary comprehension to create a new dictionary with only the common keys in both dictionaries. Then, we use all() function to compare the values of the corresponding keys in the two dictionaries.
Algorithm
1. Create a new dictionary using dictionary comprehension with only the common keys in both dictionaries.
2. Use all() function to compare the values of the corresponding keys in the two dictionaries.
3. If the values of all the keys are equal, then the two dictionaries are equal.
Python3
# sample input dictionaries dict1 = { 'Lazyroar' : 5 , 'gfg' : 1 , 'is' : 2 , 'for' : 4 , 'best' : 3 } dict2 = { 'Lazyroar' : 5 , 'gfg' : 2 , 'is' : 3 , 'for' : 7 , 'best' : 3 } # create a new dictionary with only common keys in both dictionaries common_keys = set (dict1.keys()) & set (dict2.keys()) new_dict1 = {key: dict1[key] for key in common_keys} new_dict2 = {key: dict2[key] for key in common_keys} # compare values of corresponding keys if all (new_dict1[key] = = new_dict2[key] for key in new_dict1.keys()): print ( "Dictionaries are equal" ) else : print ( "Dictionaries are not equal" ) |
Dictionaries are not equal
Time complexity: O(n + k), this code is dependent on a few factors, such as the size of the dictionaries and the number of common keys between them. The first step involves finding the common keys between the two dictionaries, which is an O(n) operation, where n is the length of the largest dictionary. Next, we create two new dictionaries with only the common keys, which also takes O(n) time as we iterate over each key in the set of common keys. we compare the values of corresponding keys, which takes O(k) time, where k is the number of common keys.
Space complexity: O(n+k), this code is also dependent on the size of the dictionaries and the number of common keys. We create two new dictionaries with only the common keys, which would have a space complexity of O(k), where k is the number of common keys. The set of common keys also takes up space, which is O(n), where n is the length of the largest dictionary.
Method 4 : use set operations
Get the common keys in both dictionaries using set intersection operation.
Create two sets, one for values of common keys in dict1 and another for values of common keys in dict2.
Check if both sets are equal.
Python3
# sample input dictionaries dict1 = { 'Lazyroar' : 5 , 'gfg' : 1 , 'is' : 2 , 'for' : 4 , 'best' : 3 } dict2 = { 'Lazyroar' : 5 , 'gfg' : 2 , 'is' : 3 , 'for' : 7 , 'best' : 3 } # get common keys in both dictionaries common_keys = set (dict1.keys()) & set (dict2.keys()) # create sets of values for common keys in both dictionaries dict1_values = {dict1[key] for key in common_keys} dict2_values = {dict2[key] for key in common_keys} # check if both sets of values are equal if dict1_values = = dict2_values: print ( "Dictionaries are equal" ) else : print ( "Dictionaries are not equal" ) |
Dictionaries are not equal
Time complexity: O(n), where n is the number of keys in the dictionaries.
Auxiliary space: O(n), to store sets of values for common keys in both dictionaries.