In this tutorial, we have two dictionaries and want to find out what they might have in common (like the same keys, same values, etc.). Basically there are some similarities between the two dictionaries and you have to find out these similarities then this article is most helpful. SO let’s start learning how to compare two dictionaries in Python and find similarities between them.
Similarities between dictionaries in Python
Basically A dictionary is a mapping between a set of keys and values. The keys support the basic operations like unions, intersections, and differences. When we call the items() method on a dictionary then it simply returns the (key, value) pair.
Now, Consider two dictionaries:
a = { 'x' : 1, 'y' : 2, 'z' : 3 } b = { 'w' : 10, 'x' : 11, 'y' : 2 }Find keys in common of two dictionaries in Python
Here, we write a code that finds the keys that are common in two dictionary as:
a = { 'x' : 1, 'y' : 2, 'z' : 3 } b = { 'w' : 10, 'x' : 11, 'y' : 2 } common_keys = a.keys() & b.keys() # intersection operation on keys print("The common keys are :",common_keys)Output :
Find keys in a dictionary A that are not in dictionary B
Now , we are finding that keys which are present in dictionary a and not in dictionary b as:
A = { 'x' : 1, 'y' : 2, 'z' : 3 } B = { 'w' : 10, 'x' : 11, 'y' : 2 } diffrent_keys = A.keys() - B.keys() print("The key that are in A and not in B are :",diffrent_keys)Output :
The key that are in A and not in B are : {'z'}Find (key,value) pairs in common in dictionaries in Python
Now, we perform the an operation by which we are finding the (key,value) pair that is common in both dictionary as:
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:
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:
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: