0

Help. Regarding dictionary Get function.

I can print "apple" if I remove/comment True = False statement. But I don't understand how I get False for "apple" with the True: False statement. pairs = { 3: "Orange", 1: "apple", 2: "Mango", True:False } print(pairs.get(3)) #Result: Orange print(pairs.get(1)) #Result: False ??? print(pairs.get(2)) #Result: Mango Would appreciate an explanation. Thanks.

14th Oct 2023, 7:31 PM
DJ Bubbly
7 Respuestas
+ 7
True == 1 so 1: "apple" gets overwritten with True: False
14th Oct 2023, 7:33 PM
Lisa
Lisa - avatar
+ 5
keys must be unique. 2 is unique 3 is unique 1 is not unique: 1 == True
14th Oct 2023, 7:50 PM
Lisa
Lisa - avatar
+ 2
Keys from dict must be unique, You already have True: False and 1 is equal to True so that s mean your dictionary is like this finally pairs = { 3: "Orange", 2:"Mango", True: False # is the same as 1: False } If you get the value of key 1, you have False
15th Oct 2023, 1:02 PM
Harimamy Ravalohery
Harimamy Ravalohery - avatar
+ 1
Lisa Thank you.
14th Oct 2023, 7:40 PM
DJ Bubbly
+ 1
Lisa Thanks so much. This has cleared my doubts.
14th Oct 2023, 7:53 PM
DJ Bubbly
15th Oct 2023, 1:08 PM
DJ Bubbly
0
DJ Bubbly you're welcome 🙂
15th Oct 2023, 1:10 PM
Harimamy Ravalohery
Harimamy Ravalohery - avatar