Python – Compare Dictionaries on certain Keys
Last Updated :
21 Apr, 2023
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
test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'geeks' : 5 }
test_dict2 = { 'gfg' : 2 , 'is' : 3 , 'best' : 3 , 'for' : 7 , 'geeks' : 5 }
print ("The original dictionary 1 : " + str (test_dict1))
print ("The original dictionary 2 : " + str (test_dict2))
comp_keys = [ 'best' , 'geeks' ]
res = True
for key in comp_keys:
if test_dict1.get(key) ! = test_dict2.get(key):
res = False
break
print ("Are dictionary equal : " + str (res))
|
Output :
The original dictionary 1 : {‘geeks’: 5, ‘gfg’: 1, ‘is’: 2, ‘for’: 4, ‘best’: 3} The original dictionary 2 : {‘geeks’: 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
test_dict1 = { 'gfg' : 1 , 'is' : 2 , 'best' : 3 , 'for' : 4 , 'geeks' : 5 }
test_dict2 = { 'gfg' : 2 , 'is' : 3 , 'best' : 3 , 'for' : 7 , 'geeks' : 5 }
print ("The original dictionary 1 : " + str (test_dict1))
print ("The original dictionary 2 : " + str (test_dict2))
comp_keys = [ 'best' , 'geeks' ]
res = all (test_dict1.get(key) = = test_dict2.get(key) for key in comp_keys)
print ("Are dictionary equal : " + str (res))
|
Output :
The original dictionary 1 : {‘geeks’: 5, ‘gfg’: 1, ‘is’: 2, ‘for’: 4, ‘best’: 3} The original dictionary 2 : {‘geeks’: 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
dict1 = { 'geeks' : 5 , 'gfg' : 1 , 'is' : 2 , 'for' : 4 , 'best' : 3 }
dict2 = { 'geeks' : 5 , 'gfg' : 2 , 'is' : 3 , 'for' : 7 , 'best' : 3 }
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}
if all (new_dict1[key] = = new_dict2[key] for key in new_dict1.keys()):
print ( "Dictionaries are equal" )
else :
print ( "Dictionaries are not equal" )
|
Output
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
dict1 = { 'geeks' : 5 , 'gfg' : 1 , 'is' : 2 , 'for' : 4 , 'best' : 3 }
dict2 = { 'geeks' : 5 , 'gfg' : 2 , 'is' : 3 , 'for' : 7 , 'best' : 3 }
common_keys = set (dict1.keys()) & set (dict2.keys())
dict1_values = {dict1[key] for key in common_keys}
dict2_values = {dict2[key] for key in common_keys}
if dict1_values = = dict2_values:
print ( "Dictionaries are equal" )
else :
print ( "Dictionaries are not equal" )
|
Output
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.
Share your thoughts in the comments
Please Login to comment...