+ 1

How can I print 'apple' out of this 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"))

24th Apr 2022, 8:57 AM
Ibrahim Alzein
Ibrahim Alzein - avatar
3 Answers
+ 3
Ibrahim , the dictionary is coded with a duplicated key, since the value of *1* and *True* are both integer values of *1*. as keys in dictionaries has to be unique, the pair *1: 'apple'* gets removed, and only the last will remain *True: False*. we can check this by: print(1 == True) which returns True if we print the complete dict by: print(pairs.items()) the result is: dict_items([(1, False), ('orange', [2, 3, 4]), (12, 'True')])
24th Apr 2022, 9:35 AM
Lothar
Lothar - avatar
+ 3
Yes, (True: False) replaced (1: "apple")
24th Apr 2022, 12:02 PM
John Wells
John Wells - avatar
+ 2
Thanks for answering... Now why when we print pairs.items() the first element is (1, False) is it because of uniqueness of the key ?!
24th Apr 2022, 9:48 AM
Ibrahim Alzein
Ibrahim Alzein - avatar