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.
The output is:
If you provide a key multiple times like so:
the output will be:
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:
The output will be:
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:
The output is:
If we slightly change the dict2 variable:
The output will become:
Similarly, you can use the “!=” operator:
The output will be:
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:
The output will be:
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:
The output now will be:
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:
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):
The output is:
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:
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.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.