Python – Compare Dictionaries on certain Keys

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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, 'geeks' : 5}
test_dict2 = {'gfg' : 2, 'is' : 3, 'best' : 3, 'for' : 7, 'geeks' : 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', 'geeks']
  
# 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)) 

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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, 'geeks' : 5}
test_dict2 = {'gfg' : 2, 'is' : 3, 'best' : 3, 'for' : 7, 'geeks' : 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', 'geeks']
  
# 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)) 

chevron_right


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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.