Python 3 compare two dictionary keys and values

In this article you can see how to compare two dictionaries in python:

  • if their keys match
  • the difference in the keys
  • the difference in the values

The first check is to verify that two dictionaries have equal keys. Below you can find code which check the keys:

d1 = {'brazil': 'Brazil', 'italy': 'Italy', 'egypt': 'Egypt', 'canada': 'Canada', 'china': 'China', 'panama': 'Panama', 'mexico': 'Mexico'}
d2 = {'cuba': 'Cuba', 'brazil': 'Brazil', 'italy': 'ItalY', 'egypt': 'Egypt', 'canada': 'canada', 'china': 'China', 'australia': 'Australia'}

check_result = set(d1.keys()) == set(d2.keys())

the result of this code is:

False

Now let say that you want to find the key difference from d1 to d2 and the reverse. This is again a simple operation which can be done by using set conversion of the key lists:

d1 = {'brazil': 'Brazil', 'italy': 'Italy', 'egypt': 'Egypt', 'canada': 'Canada', 'china': 'China', 'panama': 'Panama', 'mexico': 'Mexico'}
d2 = {'cuba': 'Cuba', 'brazil': 'Brazil', 'italy': 'ItalY', 'egypt': 'Egypt', 'canada': 'canada', 'china': 'China', 'australia': 'Australia'}

countries1_set = set(d1.keys())
countries2_set = set(d2.keys())

print (countries1_set.difference(countries2_set))
print (countries2_set.difference(countries1_set))

result:

{'mexico', 'panama'}
{'australia', 'cuba'}

Which means that first dictionary has 'mexico' and 'panama' as extra keys. You can see that order of the keys is not preserved.

Now let us compare two dictionaries based on keys and output information for the values:

d1 = {'brazil': 'Brazil', 'italy': 'Italy', 'egypt': 'Egypt', 'canada': 'Canada', 'china': 'China', 'panama': 'Panama', 'mexico': 'Mexico'}
d2 = {'cuba': 'Cuba', 'brazil': 'Brazil', 'italy': 'ItalY', 'egypt': 'Egypt', 'canada': 'canada', 'china': 'China', 'australia': 'Australia'}

k1 = set(d1.keys())
k2 = set(d2.keys())
common_keys = set(k1).intersection(set(k2))

for key in common_keys:
    if d1[key] != d2[key] :
      print (key + ":" + str(d2[key]) + " match " + str(d1[key]))

the result is:

italy:ItalY match Italy
canada:canada match Canada

As you can see we match the values from the both dictionaries for the same key. This is helpful when you have difference between the dictionaries and you don't want to lose information. You can check how to merge two dictionaries here:
Python How to Merge Dictionaries - examples for beginners

Now if you want to compare two dictionaries based on their values then you can use:

d1 = {'brazil': 'Brazil', 'italy': 'Italy', 'egypt': 'Egypt', 'canada': 'Canada', 'china': 'China', 'panama': 'Panama', 'mexico': 'Mexico'}
d2 = {'cuba': 'Cuba', 'brazil': 'Brazil', 'italy': 'ItalY', 'egypt': 'Egypt', 'canada': 'canada', 'china': 'China', 'australia': 'Australia'}

countries1_set = set(d1.values())
countries2_set = set(d2.values())

print (countries1_set.difference(countries2_set))
print (countries2_set.difference(countries1_set))

result:

{'Mexico', 'Canada', 'Italy', 'Panama'}
{'ItalY', 'Australia', 'Cuba', 'canada'}

Which as you can see is case sensitive! In order to make it case insensitive you can use conversion of the values with:

d1 = {'brazil': 'Brazil', 'italy': 'Italy', 'egypt': 'Egypt', 'canada': 'Canada', 'china': 'China', 'panama': 'Panama', 'mexico': 'Mexico'}
d2 = {'cuba': 'Cuba', 'brazil': 'Brazil', 'italy': 'ItalY', 'egypt': 'Egypt', 'canada': 'canada', 'china': 'China', 'australia': 'Australia'}

countries1_set = set([v.lower() for v in d1.values()])
countries2_set = set([v.lower() for v in d2.values()])

print (countries1_set.difference(countries2_set))
print (countries2_set.difference(countries1_set))

result:

{'mexico', 'panama'}
{'australia', 'cuba'}

Related Article