How to make a dictionary with duplicate keys in Python

Dictionaries do not support duplicate keys. However, more than one value can correspond to a single key using a list. For example, with the dictionary {“a”: [1, 2]}, 1 and 2 are both connected to the key “a” and can be accessed individually.

Solution for How to make a dictionary with duplicate keys in Python : You can use a list in a dictionary to associate more than one value with a key Create a dictionary with lists as values. Use a list to contain more than one value. Though the key isn’t duplicated, multiple values are now assigned to the same key.

To add a value to an existing key, call the key and append to the associated list.

a_dictionary = {"a": [1, 2], "b": [3, 4]}


how-to-make-a-dictionary-with-duplicate-keys-in-python