dictionaries | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

dictionaries

this my code fruits={1:"apple","orange":[2,3,4],True:False,None:"true",} print(fruits.get(1)) OUTPUT >> False what's the mistake in code when i replace with 1 with 0. i m getting out put as "apple". somebody help...

30th May 2018, 10:32 AM
Aju George
Aju George - avatar
2 Answers
+ 6
This is because there are conflicting keys in your dictionnary. This may not be obvious but 1 and True represents the same thing because in Python the boolean True equates the integer 1 and False equates the integer 0. So when you do fruits.get(1) since you have a conflict it gets the latest entry with that key, at least that is what I have seen happen. Run this code to see what I mean. For each list look at the order of the entries and check what the output is. https://code.sololearn.com/c7a6sNRDtNm7/?ref=app
30th May 2018, 10:50 AM
cyk
cyk - avatar
+ 2
thanku ☺
30th May 2018, 10:54 AM
Aju George
Aju George - avatar