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)
14 Respostas
+ 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.
+ 2
1 == True, so apple will be overwritten with False
+ 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
+ 2
1: "apple" will be overwritten by True: False or 1: False if you want to put it like that
+ 2
1 is the same as True, keys must be unique, so 'apple' will be overwritten by the following False
+ 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?
+ 1
False comes after "apple" there "apple" is overwritten by False
+ 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.
0
Lisa If 1 == True, why would apple be overwritten?
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?
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" ?
0
Hello Bhai muze aapki thodisi help chahiye thi....
0
Kolasn Bolo bhai ... Kya seva karu?