+ 1
Fetching key value in dictionary! Why in dictionary keys 1 and True are same
pairs = {1: "apple", "orange": [2, 3, 4], True: False, 2: "True", } print(pairs.get("orange")) print(pairs.get(7)) print(pairs.get(1)) print(pairs.get(2)) Why third print is not fetching key value "apple" While fourth p is getting output
5 Respuestas
+ 3
So I learned that if dictionary contains 1 and True as key then it will work as 1== True
Else dictionary containing only 1 as key fetches it's value
+ 5
The dictionary contains 1 and True. As True equals 1 as key, True: False over writes 1: "apple".
That's why get(1) returns False.
+ 3
Here is a similar question with answers:
https://www.sololearn.com/Discuss/2760597/?ref=app
+ 1
Zahed Shaikh
True means 1 so True == 1 will return True
Actually you are doing
pairs.get(1) = pairs.get(True)
0
Why is that keys 1 and True are same in dictionary