Kodeclik Blog
How to check if two dictionaries are equal in Python
Recall that dictionaries, also called associative arrays, are collections of (key, value) pairs. Here is some simple code to create a dictionary mapping numbers to letters.
dict1 = {1: 'A', 2: 'B', 3: 'C', 26: 'Z'}
print(dict1)
The output is:
{1: 'A', 2: 'B', 3: 'C', 26: 'Z'}
If you provide a key multiple times like so:
dict1 = {1: 'A', 2: 'B', 3: 'C', 2: 'Z'}
print(dict1)
the output will be:
{1: 'A', 2: 'Z', 3: 'C'}
Note that the keys have to be unique so the earlier assignment mapping the key of 2 to the value of ‘B’ is overwritten to map to the value of ‘Z’.
You can create multiple dictionaries like so:
dict2 = {2: 'B', 3: 'C', 26: 'Z', 1: 'A'}
print(dict2)
The output will be:
{2: 'B', 3: 'C', 26: 'Z', 1: 'A'}
Note that the above variable dict2 is really the same as our first definition of variable dict1. (The order of the keys doesn’t matter.) How can we check formally within Python that two dictionaries are really one and the same?
Method 1: Use the comparison operators == and !=
Here is some very simple code using == to compare the two dictionaries:
dict1 = {1: 'A', 2: 'B', 3: 'C', 26: 'Z'}
dict2 = {2: 'B', 3: 'C', 26: 'Z', 1: 'A'}
print(dict1==dict2)
The output is:
True
If we slightly change the dict2 variable:
dict1 = {1: 'A', 2: 'B', 3: 'C', 26: 'Z'}
dict2 = {2: 'B', 3: 'C', 26: 'Z', 4: 'A'}
print(dict1==dict2)
The output will become:
False
Similarly, you can use the “!=” operator:
dict1 = {1: 'A', 2: 'B', 3: 'C', 26: 'Z'}
dict2 = {2: 'B', 3: 'C', 26: 'Z', 4: 'A'}
print(dict1!=dict2)
The output will be:
True
One of the limitations of these comparison operators is that they give a boolean yes/no answer but cannot highlight the differences between the dictionaries. For that purpose, we turn to some more advanced functions.
Method 2: Use the DeepDiff module
DeepDiff is a module that you will need to install first (e.g., using a command like “pip install deepdiff”) and then you can import functions from it in your program:
from deepdiff import DeepDiff
dict1 = {1: 'A', 2: 'B', 3: 'C', 26: 'Z'}
dict2 = {2: 'B', 3: 'C', 26: 'Z', 4: 'A'}
differences = DeepDiff(dict1,dict2)
print(differences)
The output will be:
{'dictionary_item_added': [root[4]],
'dictionary_item_removed': [root[1]]}
As the output shows, in going from dict1 to dict2, we see that a dictionary item has been added (namely the one with key=4) and a dictionary item has been removed (namely the one with key=1).
Here’s another example with DeepDiff:
from deepdiff import DeepDiff
dict1 = {1: 'A', 2: 'B', 3: 'C', 26: 'Z'}
dict2 = {2: 'B', 3: 'C', 26: 'Z', 1: 'a'}
differences = DeepDiff(dict1,dict2)
print(differences)
The output now will be:
{'values_changed': {'root[1]':
{'new_value': 'a', 'old_value': 'A'}}}
Note that in this example, there is no modification of keys, i.e., no new keys were introduced from dict1 to dict2 and no existing keys were deleted. Instead the values are changed from upper case ‘A’ to lower case ‘a’.
Ignoring string case in DeepDiff
DeepDiff takes many parameters; for instance we can specify "ignore_string_case" to be True to indicate that we do not care about stringcase. Let us try that:
from deepdiff import DeepDiff
dict1 = {1: 'A', 2: 'B', 3: 'C', 26: 'Z'}
dict2 = {2: 'B', 3: 'C', 26: 'Z', 1: 'a'}
differences = DeepDiff(dict1,dict2,ignore_string_case=True)
print(differences)
The output now will be:
{}
indicating there are no differences via the empty set.
Let us make some more small modifications to our dictionaries (note the changes carefully):
from deepdiff import DeepDiff
dict1 = {1: 'A', 2: 'B', 3: 'C', 26: 45}
dict2 = {2: 'B', 3: 'C', 26: '45', 1: 'a'}
differences = DeepDiff(dict1,dict2,ignore_string_case=True)
print(differences)
The output is:
{'type_changes': {'root[26]':
{'old_type': <class 'int'>, 'new_type': <class 'str'>,
'old_value': 45, 'new_value': '45'}}}
As can be seen, for the key 26, the old type is an integer (namely 45) whereas the new type is the string ‘45’. The ignore_string_case=True setting does not cover these differences.
Ignoring keys in DeepDiff
DeepDiff has a parameter if you would like to ignore some fields (i.e., keys) in identifying differences. Consider the following code:
from deepdiff import DeepDiff
dict1 = {1: 'A', 2: 'B', 3: 'C', 26: 45}
dict2 = {2: 'B', 3: 'C', 26: '45', 1: 'a'}
differences = DeepDiff(dict1,dict2,
ignore_string_case=True,
exclude_paths=["root[26]"])
print(differences)
The output now will be:
{}
In other words, if we ignore case and we also ignore the key=26 entries, the two dictionaries are equal.
In this blogpost you have learnt two powerful ways to check if given dictionaries are equal in Python. Experiment with your own examples to get the hang of these functions!
Once you are comfortable with dictionaries, learn how to increment a value in a Python dictionary, and how to find the first key in a Python dictionary. Also learn how to check if a key exists in a Python dictionary.
Some of the approaches presented here can be used in assertion code to verify equality before further computations. Learn about assertions and the Python assertEqual() function.
Interested in more things Python? See our blogposts on Python's enumerate() capability and how to take multiline input in Python. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.