Why is the value of the first key "apple" deleted in the dictionary? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the value of the first key "apple" deleted in the dictionary?

pairs = {1 : "apple", "orange" : [2, 3, 4], True : False, 12 : "True", } print(pairs.get("orange")) print(pairs.get(7, 42)) print(pairs.get(12345, "not found")) pairs.pop('orange') print(pairs)

6th Mar 2022, 9:58 AM
Ashok Ighe
Ashok Ighe - avatar
14 Answers
+ 2
Ok let me try again. At first, your dictionary is given this key:value pair... 1:"apple" and then it is given this key and value... True:False But the second assignment is actually saying this... 1:False (since boolean True is the same as 1) So, you have now done two assignments of a value to the SAME key in the SAME dictionary; therefore the 2nd assignment OVERWRITES the first. That is, the value (for the key of 1) is no longer "apple" but is now False. There is no such thing as a key of True, distinct from a key of 1. Those are just two ways of indicating the same key.
9th Mar 2022, 1:55 PM
Erik
+ 2
1 == True, so apple will be overwritten with False
6th Mar 2022, 10:07 AM
Lisa
Lisa - avatar
+ 2
1 : "apple" #here 1 evaluates to True . True : "apple" like this. True : false #as 1 evaluates to True . so now changing the value of key True will override the value of 1 So at last value of 1 is False
6th Mar 2022, 10:08 AM
TOLUENE
TOLUENE - avatar
+ 2
1: "apple" will be overwritten by True: False or 1: False if you want to put it like that
6th Mar 2022, 10:14 AM
Lisa
Lisa - avatar
+ 2
1 is the same as True, keys must be unique, so 'apple' will be overwritten by the following False
6th Mar 2022, 10:25 AM
Lisa
Lisa - avatar
+ 2
Lisa Erik TOLUENE Let me be very very clear. What I understand: 1) 1 is equal to True, and 0 is equal to False. 2) I understand that Python will read 1 as True. What I don't understand: 1) Why Python also changed "apple" to False? Why cant it be - True: "apple" ? What is the reason behind changing apple to False?
8th Mar 2022, 6:55 AM
Ashok Ighe
Ashok Ighe - avatar
+ 1
False comes after "apple" there "apple" is overwritten by False
6th Mar 2022, 6:17 PM
Lisa
Lisa - avatar
+ 1
You are assigning a value twice to the SAME key (that is, 1). So the first value (apple) gets replaced by the new value (False) from the second assignment. 1 and True are not distinct keys, because as you noted, they have the same hash value, so they are two ways of writing the same thing.
7th Mar 2022, 2:28 PM
Erik
+ 1
Erik Lisa Simba TOLUENE My doubt is solved! Thank you very much guys for your time. I appreciate it ❤️
12th Mar 2022, 4:30 AM
Ashok Ighe
Ashok Ighe - avatar
0
Lisa If 1 == True, why would apple be overwritten?
6th Mar 2022, 10:11 AM
Ashok Ighe
Ashok Ighe - avatar
0
TOLUENE 1) Why changing the value of key will override it's value also? 2) Why can't it be: True : "apple" ? 3) Why it changes the key to True? Why can't it be an integer 1?
6th Mar 2022, 10:23 AM
Ashok Ighe
Ashok Ighe - avatar
0
Lisa I still don't get it. I understand that 1 and True has same hash values. What I don't understand is, why "apple" needs to be overwritten with False? Why can't it be just "apple" ?
6th Mar 2022, 6:15 PM
Ashok Ighe
Ashok Ighe - avatar
0
Hello Bhai muze aapki thodisi help chahiye thi....
8th Mar 2022, 9:53 AM
Kolasn
0
Kolasn Bolo bhai ... Kya seva karu?
8th Mar 2022, 3:25 PM
Ashok Ighe
Ashok Ighe - avatar