Practice Question: Dict Keys and Equality

What is the output of the following code snippet?

Python
d = {True: "a", 1.0: "b", 1: "c"}
print(d)
Answer

The output is:

Python
{1: "c"}

Explanation:

All three keys True, 1.0, and 1 are treated as the same key in Python because they all have equal value, which is a sufficient condition for key equivalence.

Since dictionary keys must be unique, each new assignment overwrites the previous one. Therefore, the final surviving key:value pair is 1: "c".

Back to collection