Line4 is a problem. It assumes there is a single default value and assigns that to each. In your case that means each key is assigned the same empty list. When any key is updated, all the keys get updated because they're sharing the same list.
There are several ways to do this, but I might use setdefault(). I would append the position to the output of setdefault(). It will be the list if it already exists, or an empty list if it doesn't.
Your print line doesn't have parentheses. Are you running this under python2 or python3?
There are several ways to do this, but I might use setdefault(). I would append the position to the output of setdefault(). It will be the list if it already exists, or an empty list if it doesn't.
Your print line doesn't have parentheses. Are you running this under python2 or python3?
l = ["a","b","c","a","c"] #create a dictionary d = {} #assign key and value by for loop enum = enumerate(l) for i,j in enum: d.setdefault(j, []).append(i) print (d)
Output:{'a': [0, 3], 'b': [1], 'c': [2, 4]}