I tried this example and it worked fine: Map two lists into a dictionary in Python
But if I replaced “keys” with this list:
[‘2’, ‘3’, ‘2’, ‘3’, ‘4’, ‘2’, ‘4’, ‘2’, ‘2’, ‘3’, ‘2’, ‘3’, ‘4’]
and “values” with this list:
[2, 3, 4, 5, 6, 4, 2, 1, 1, 2, 3, 4, 5]
The dict class tried to make a dictionary of the “values” part alone!
OUTPUT:
{‘3’: 4, ‘2’: 3, ‘4’: 5}
Try it yourselves, you should get the same answer. A why and an alternative to this would be great.
A dict requires that there are no duplicate keys, but in your keys list you have lots of duplicates, so the dict will merge them. If you want to have multiple keys, you can use a multidict implementation to achieve that:
Note: I used
paste.util.multidict.MultiDict, installabe bypip install paste.