Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

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:

basket1 = { 'apple': 1.5, 'orange': 2, 'banana': 0.5}
basket2 = {'orange': 2, 'banana': 0.5, 'apple': 1.5}
basket3 = {'apple': 1.5, 'banana': 0.5, 'pear': 2, 'orange': 2}

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 two Python dictionaries

Comparing dictionaries using == and !=

Here are some simple comparisons of the above dictionaries using the == operator:

basket1 = {'apple': 1.5, 'orange': 2, 'banana': 0.5}
basket2 = {'orange': 2, 'banana': 0.5, 'apple': 1.5}
basket3 = {'apple': 1.5, 'banana': 0.5, 'pear': 2, 'orange': 2}

print(basket1 == basket2)
print(basket1 == basket3)
print(basket2 == basket3)

The output is:

True
False
False

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):

basket1 = {'apple': 1.5, 'orange': 2, 'banana': 0.5}
basket2 = {'orange': 2, 'banana': 0.5, 'apple': 1.5}
basket3 = {'apple': 1.5, 'banana': 0.5, 'pear': 2, 'orange': 2}

print(basket1 != basket2)
print(basket1 != basket3)
print(basket2 != basket3)

The output is:

False
True
True

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:

basket1 = {'apple': 1.5, 'orange': 2, 'banana': 0.5}
basket2 = {'apple': 2, 'orange': 0.5, 'banana': 1.5}

print(basket1 == basket2)
print(basket1.keys() == basket2.keys())
print(basket1.values() == basket2.values())

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:

False
True
False

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:

basket1 = {'apple': 1.5, 'orange': 2, 'banana': 0.5}
basket2 = {'apple': 2, 'orange': 0.5, 'banana': 1.5}

print(basket1 == basket2)

bk1 = list(basket1.keys())
bk2 = list(basket2.keys())
print(bk1.sort() == bk2.sort())

bv1 = list(basket1.values())
bv2 = list(basket2.values())
print(bv1.sort() == bv2.sort())

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:

False
True
True

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.

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.

Kodeclik sidebar newsletter

Join our mailing list

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

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2024. All rights reserved.