+ 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"))
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')])
+ 3
Yes, (True: False) replaced (1: "apple")
+ 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 ?!