This question only uses a single index for each element. I am trying to do the same but with repetitions in the list, so the values in the dict will be lists of indexes.
I have the following code that reduces a list to a dict:
def list_to_dict(l):
d = {}
for i in range(len(l)):
if l[i] not in d:
d[l[i]] = []
d[l[i]].append(i)
return d
print(list_to_dict([1, 2, 3, 1]))
# Gives: {1: [0, 3], 2: [1], 3: [2]}
May I know if there is a more pythonic/efficient way to do this?