Example of my Code:
dictionary={ 'key1' : 'value1' , 'key2' : 'value2' , 'key1' : 'value3' }
x=dictionary['key1']
print(x)
This prints ONLY value3. I want it to print both value3 and value1, since they
both correlate to key1. How can I do this?
You cannot. The keys are added to the dictionary in the order you specified and the second occurence of ‘key1’ overwrites the previous one.
This is just as though you had written:
and then asked how you could print both values of
x.To record multiple values the easiest way is to use a
defaultdictand build lists of values: