Dictionaries having integer and boolean as keys

Screen Link:

dictionary key as int and bool
My Code:

In explanation above this image it is mentioned that 
"An odd "gotcha" is when we mix integers with Booleans as dictionary keys. The hash() command converts the Boolean True to 1, and the Boolean False to 0. This means the Booleans True and False will conflict with the integers 0 and 1. The dictionary keys won't be unique anymore, and Python will only keep the last key-value pair in cases like that."

[Link to the exercise](https://app.dataquest.io/m/314/dictionaries-and-frequency-tables/5/key-value-pairs)

What I expected to happen:
As the last line says “The dictionary keys won’t be unique anymore, and Python will only keep the last key-value pair in cases like that” the output of d_1 should be {True: ‘Boolean’},
d_2 should be {0: ‘zero’} etc

What actually happened:
The output is something different as we see in the screenshot.
Please explain in detail how is the output the way it is.

Replace this line with the output/error

Hey krrishd1007,
Python considers 1==1.0==True because Python treats ‘bool’ as a subclass of ‘int’ and in your case,the d_1 dictionary has two keys;1 and True which are considered equal by Python.Finally after printing, you get a result where the key is the first hash and value is the last one.This occurs for 0 and False also!

3 Likes

Thank you Moumita, the answer was helpful.