Kodeclik Blog

Comparing two Python dictionaries

Recall that dictionaries are basically associative arrays mapping keys to values in Python. Below are a few dictionaries representing prices of fruits:
Note that dictionaries are by definition unordered even though they are specified in some order and printed in a given order. Thus, for instance, basket1 and basket2 are really the same, even though the order of fruits is different.
So how do we go about comparing two Python dictionaries?

Comparing dictionaries using == and !=

Here are some simple comparisons of the above dictionaries using the == operator:
The output is:
As you can see basket1 and basket2 are deemed equal but basket3 is different from both basket1 and basket2 (because it has an additional fruit entry).
You can also use the “!=” operator (with results exactly the opposite of what we have above):
The output is:
showing again that basket1 is the same as basket2.

Comparing dictionaries using keys() and values()

keys() and values() are two functions you can use to extract just the keys or just the values from a dictionary. Thus, another way to compare dictionaries is to compare just the keys or just the values of dictionaries. Consider the following code:
Here we have two baskets containing the same keys and the same values but in different associations. In the first basket apple is paired with 1.5 whereas in the second basket banana is paired with 1.5, etc. Thus the two baskets are different and the regular == operator studied above should yield a False. Let us also compare the keys() and values() as shown in the last two lines of the program. When we run this program, we get:
Wow - what happened? The first line gives False as expected, The second line’s True is also explainable - as mentioned above, the keys are the same and thus the two sets of keys are the same. But why do we get False when comparing values when the values are really the same?
The reason is that keys() and values() return not a set but a class called 'dict_keys' or 'dict_values' (respectively). As a result the order in which they return their keys or values makes a difference. Thus, even though the values (and keys) are the same we cannot rely on values() and keys() to return them in some canonical order for ease of comparison. You can instead convert them to a list, sort the list yourself into an order, and then compare:
Here, we create two lists bk1 and bk2 comprising the keys. Similarly, we create two lists bv1 and bv2 comprising the values. Then we sort these lists before comparison. Now we will obtain:
as expected.
So we have learned how to compare two dictionaries in Python using the == and != operators and also how to do more fine-grained comparison using the keys() and values() methods. With the latter approach you might need to convert the results of these methods to lists and sort them before comparison.
The moral of the story is to think carefully about the type of comparison you are looking for and then choose the right method based on your desired approach.
If you liked this blogpost, learn how to unpack a dictionary into variables and how to slice dictionaries.
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. 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 2023. All rights reserved.