Can a dictionary repeat identical keys or values?


What happens in a dictionary if you have repeated keys or repeated values?


Each of these is a separate case. Multiple keys can all have the same value as demonstrated below. This causes no issues.

>>> my_dict = { 'key' + str(x+1): 0 for x in range(10) }
>>> my_dict
{'key1': 0, 'key2': 0, 'key3': 0, 'key4': 0, 'key5': 0, 'key6': 0, 'key7': 0, 'key8': 0, 'key9': 0, 'key10': 0}

However you cannot have identical keys within a dictionary. Attempting to create a second identical key will simply overwrite the first as shown below.

>>> my_dict = { 'key': x for x in range(10) }
>>> my_dict
{'key': 9}

Special reminder that 6 is not the same as '6' as one is numeric and the other a string, so if these were each a key they would be unique from each other.

Credit for examples used goes to @mtf.