Make a dictionary with duplicate keys in Python

后端 未结 7 1031
野的像风
野的像风 2020-11-22 00:37

I have the following list which contains duplicate car registration numbers with different values. I want to convert it into a dictionary which accepts this multiple keys of

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 01:12

    You can't have a dict with duplicate keys for definition! Instead you can use a single key and, as value, a list of elements that had that key.

    So you can follow these steps:

    1. See if the current element's (of your initial set) key is in the final dict. If it is, go to step 3
    2. Update dict with key
    3. Append the new value to the dict[key] list
    4. Repeat [1-3]

提交回复
热议问题