Can a dictionary have two keys of the same value?


#1

Question

Can a dictionary have two keys with the same value?

Answer

No, each key in a dictionary should be unique. You can’t have two keys with the same value. Attempting to use the same key again will just overwrite the previous value stored. If a key needs to store multiple values, then the value associated with the key should be a list or another dictionary.


#2

That each key should be unique is easy to understand. The keys of a dictionary themselves form set so each will have their own unique name. However, the values are not bound by this rule. Every key can have the same value associated with it. Values do not comprise a set of anything. They are plain data. It’s the keys that are unique. If a new value is assigned to a key that exists already, it will overwrite the old value.