Dictionaries output

Dear all,
if the dictionary d_2 ={False:“Boolean”, 0:“zero”}, what should output to be?
should it to be {0:“zero”} or {False:“zero”}

Thank you

Short answer, it will take the last value since both keys are the same. Then the last value get updated.

First, you need to know that only values that are hash-able are allowed to be use as a key for the dictionary. Immutable object are hash able - I.e. integers, strings, tuple.

Under the hood, dictionary are implemented as a hash table.

When you use False as a key for the dictionary, False will be evaluated as integer 0. Bool are a sub class of integer objects. Hence inherits the property of being hash able. Integer are hash-able. False’s integer equivalent is 0.

Keys opens/maps to letter boxes. Each unique key has its own letter boxes.

The letter box is used to store data.

key = 0, value = 10, key 0 gets maps to a letter box that contains 10.

1 Like

Thanks a lot for your reply
so, the output will be ???

^ This is the answer.

When the expression is parsed by the interpreter, the first {key : value} pair is inserted into the dictionary: {False: "Boolean"} Then, when the interpreter moves on to the second {key : value} pair in the expression, it first checks whether the key already exists.

The second key is the integer 0. As @alvinctk pointed out, False evaluates to 0 under the hood because the Boolean type is a subtype of the integer type. Try running the code False == 0 to see what happens!

So, the interpreter sees that the key already exists and simply updates its value to “zero”. The name of the key is still False though. It does not get updated to 0.

As @alvinctk said, in a dictionary assignment with repeated keys, the last value is the one that will be kept.
>>> my_dict = {"a": 0, "a": 1, "a": 2}
{"a" : 2}

2 Likes

Hey @drabdelkarim8290 !

Great question - glad our awesome moderator @alvinctk & Technical Content Editor @Slavina could help out. Next time you ask a question, please be sure to copy/paste the code you’ve tried, the output, and any other relevant information we see in this picture - instead of the picture itself!

This will help us get you an answer even faster since we’ll be able to copy/paste your code and try to reproduce. Looking forward to more amazing contributions from you!

Mary

2 Likes

thank you very much for your clarification

2 Likes

Hey @drabdelkarim8290,

We have a solved feature that allows you the ability to mark something as the “correct” answer, which helps future students with the same question quickly find the solution they’re looking for.

Here’s an article on how to mark posts as solved - I don’t want to do this for you until I know that solution/explanation works.

The image which was shared by drabdelkarim8290 can be found on this mission page: https://app.dataquest.io/m/314/dictionaries-and-frequency-tables/5/key-value-pairs

It is the last image in the tutorial, and the output should read as {0: ‘zero’} and not {False: ‘zero’}. This is for two reasons. A dictionary will convert a Boolean to a 1 or 0. True is converted to 1 and False is converted to 0. Also, a dictionary input will use the last value input for a key as the stored value for that key. The last input for the key 0 (which is also the key for False) was ‘zero’. So, the output should read {0: ‘zero’}

I believe the image in the tutorial needs to be updated as to not cause further confusion for those learning the basics of Python.

Best,
Joshua C.