-1

I am trying to get the dictionary from two lists

keys = [1, 1, 2]
values = [1, 2, 3]

I expect the result would be like this

dictionary1 = {1: 1, 2, 2:3}

I tried to do

for i, j in zip(keys, values):
    dictionary1[i].append(j)

but it did not work

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
George
  • 9
  • 2
    Does this answer your question? [How to merge dicts, collecting values from matching keys?](https://stackoverflow.com/questions/5946236/how-to-merge-dicts-collecting-values-from-matching-keys) – NIKUNJ PATEL Feb 22 '23 at 05:52
  • 3
    having a dict of `{1: 1,2, 2:3}` isn't possible as it does not fit dictionary syntax. If you are looking for a contained list or other iterable such as `{1:[1,2], 2:[3]}` that can be done. – Shorn Feb 22 '23 at 05:53

1 Answers1

0

Python does not support duplicate keys. So when a two particular lists are converted to a dictionary like :

keys = [1, 1, 2]
values = [1, 2, 3]
dictionary1 = dict(zip(keys, values))
print(dictionary1)

It will output only the latest reference given to it.

{1: 2, 2: 3}