Why does this code print {True: 'maybe'} ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Why does this code print {True: 'maybe'} ?

https://code.sololearn.com/cFBFAySD5LuJ/?ref=app Why is the first key associated with the last value?

6th Dec 2019, 11:19 PM
Sonic
Sonic - avatar
15 Answers
- 5
Hridoy
8th Dec 2019, 8:38 PM
āĻšā§ƒāĻĻāĻ¯āĻŧ āĻ•āĻžāĻāĻĻāĻžāĻ¨ā§‹ āĻ­āĻžāĻ˛āĻŦāĻžāĻ¸āĻž
āĻšā§ƒāĻĻāĻ¯āĻŧ āĻ•āĻžāĻāĻĻāĻžāĻ¨ā§‹ āĻ­āĻžāĻ˛āĻŦāĻžāĻ¸āĻž - avatar
+ 5
Petr thanks, so the keys are effectively the same as True is 1? The last value overrides the previous values but why don't the keys specified later override the first instance of True? E.g. why is the answer not {1.0: 'maybe'} ?
6th Dec 2019, 11:51 PM
Sonic
Sonic - avatar
+ 5
Petr OK, thanks.
7th Dec 2019, 12:00 AM
Sonic
Sonic - avatar
+ 4
Sonic Try it easier: dict={1:'no',1.0:'maybe'} print(dict) the result will help you understand the secret of this dict
6th Dec 2019, 11:26 PM
Petr
+ 4
Sonic about the keys, I agree with you. Regarding the second, assignment by a new key overwrites it. true = 1, i.e. the first key is displayed as in the dictionary, and the value is the last assigned to it by this key value
6th Dec 2019, 11:57 PM
Petr
+ 4
To understand, it is necessary to know how Python build dicts. In the first example, it will create the dict in the order specified : first 'True : "yes"'. At this point, when python see '1 : "no"', the key True is already created. And that changes all. In python, for instance, you can write both : while 1 or while True ; that means that Python evaluates the value 1 as True, even 1.0. So, it can't create a new key since key 'True' already exists.
7th Dec 2019, 6:05 AM
ThÊophile
ThÊophile - avatar
+ 4
If the hash value of two objects is the same and they check as equal, they'll be considered as one and the same thing for dict purposes. So if one of them is already in there, it will not be overwritten, but the value will just be updated. Run (and meditate over) this: class StrangeThing: def __repr__(self): return '^_^' def __hash__(self): return 1 def __eq__(self, other): return True d = {} for obj in StrangeThing(), 1, 1.0, True: print(hash(obj)) d[obj] = 'x' print(d)
7th Dec 2019, 9:50 AM
HonFu
HonFu - avatar
+ 2
I got this from a challenge question. Unsure about the rule here.
6th Dec 2019, 11:20 PM
Sonic
Sonic - avatar
- 1
НаĐŋиŅˆŅƒ Ņ…ĐžŅ‚ŅŒ Ņ‡Ņ‚Đž Ņ‚Đž ĐŊĐ° ŅĐ˛ĐžĐĩĐŧ ŅĐˇŅ‹ĐēĐĩ
8th Dec 2019, 8:02 PM
MUSTAFIN ILNAR
- 2
Hi
8th Dec 2019, 8:38 PM
āĻšā§ƒāĻĻāĻ¯āĻŧ āĻ•āĻžāĻāĻĻāĻžāĻ¨ā§‹ āĻ­āĻžāĻ˛āĻŦāĻžāĻ¸āĻž
āĻšā§ƒāĻĻāĻ¯āĻŧ āĻ•āĻžāĻāĻĻāĻžāĻ¨ā§‹ āĻ­āĻžāĻ˛āĻŦāĻžāĻ¸āĻž - avatar
- 2
āĻ†āĻŽāĻžāĻ° āĻ­āĻŋāĻĄāĻŋāĻ“āĻ¤ā§‡ āĻ˛āĻžāĻ‡āĻ• āĻ•āĻŽā§‡āĻ¨ā§āĻŸ āĻļā§‡āĻ¯āĻŧāĻžāĻ° āĻ¸āĻŦāĻžāĻ‡ āĻ•āĻ°āĻŦā§‡āĻ¨ āĻ¨āĻ¤ā§āĻ¨ āĻ­āĻŋāĻĄāĻŋāĻ“ āĻ†āĻĒāĻ˛ā§‹āĻĄ āĻ•āĻ°āĻž āĻšāĻŦā§‡
8th Dec 2019, 8:39 PM
āĻšā§ƒāĻĻāĻ¯āĻŧ āĻ•āĻžāĻāĻĻāĻžāĻ¨ā§‹ āĻ­āĻžāĻ˛āĻŦāĻžāĻ¸āĻž
āĻšā§ƒāĻĻāĻ¯āĻŧ āĻ•āĻžāĻāĻĻāĻžāĻ¨ā§‹ āĻ­āĻžāĻ˛āĻŦāĻžāĻ¸āĻž - avatar
- 2
Hlo
9th Dec 2019, 3:20 AM
Bandr Sohaib
Bandr Sohaib - avatar
- 2
Kon
9th Dec 2019, 3:20 AM
Bandr Sohaib
Bandr Sohaib - avatar
- 3
Hlo
8th Dec 2019, 10:30 AM
Bandr Sohaib
Bandr Sohaib - avatar
- 3
Ų‚ØĢØĩØĩ
8th Dec 2019, 11:03 PM
Ø­ØŗŲŠŲ† Ø­ØŗŲŠŲ†
Ø­ØŗŲŠŲ† Ø­ØŗŲŠŲ† - avatar