You just need to create d1[k]
as a list.
for k in set(Key):
d1[k] = []
...
Although, the inner list comprehension is redundant: all it really does is rename j
to i
. You can unwrap it too:
for k in set(Key):
d1[k] = []
for i, x in enumerate(Key):
if x == k:
d1[k].append(Value[i])
That said, this whole thing is overcomplicated and can be done much more easily with zip
:
d1 = {}
for k, v in zip(Key, Value):
values = d1.setdefault(k, [])
values.append(v)
print(d1) # -> {'a': [1, 4], 'b': [2], 'c': [3, 5]}
Although, if Key
and Value
aren't guaranteed to be the same length, this may fail silently. To avoid that, in Python 3.10+ you could use zip(Key, Value, strict=True)
, otherwise you could rework it to be for i, k in enumerate(Key) ... values.append(Values[i])
.
Note: In Python 3.7+, this solution preserves insertion order where yours doesn't due to the set
conversion.