PYTHON - Dictionaries get print(pairs.get(1)) why = True, but 1,2 and 3 not??? Why not output = "apple" | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

PYTHON - Dictionaries get print(pairs.get(1)) why = True, but 1,2 and 3 not??? Why not output = "apple"

pairs = {1: "apple", 2: "pear", 3: "apricot", True: "True", 4: "potatoe" } print(pairs.get(1)) print(pairs.get(2)) print(pairs.get(3)) print(pairs.get(4)) print("\n") Why is print(pairs.get(1)) Output= True and not apple 2,3,4 are rational outputs but the first output totally confused? Pls reply, Regards, Philip

28th Nov 2020, 4:13 PM
Philip
2 ответов
+ 2
Because 1 and True mean the same thing......keys must be unique and as True is later on in the dict...the 1 is removed...print out the dict to see what I mean. just do add..... dictionary can be used to remove duplicate elements in the list and is faster than set(). and (I think) the ordering is kept.
28th Nov 2020, 4:23 PM
rodwynnejones
rodwynnejones - avatar
0
@rodwynnejones thanks for the info, I've written new code to explain it to myself
30th Nov 2020, 10:18 AM
Philip