Hi,
I am trying to get a dictionary from a list like ["a", "b", "c", "a", "c"]. The out come is expected to be {"a": [0, 3], "b": [1], "c": [2, 4]}. The element of the list is the key and index of the element is the value. My script below doesn't work. Need some help with why it doesn't work and a solution. Thanks!
I am trying to get a dictionary from a list like ["a", "b", "c", "a", "c"]. The out come is expected to be {"a": [0, 3], "b": [1], "c": [2, 4]}. The element of the list is the key and index of the element is the value. My script below doesn't work. Need some help with why it doesn't work and a solution. Thanks!
l = ["a","b","c","a","c"] #create a dictionary with l's elements d ={}.fromkeys(l, []) #assign key and value by for loop enum = enumerate(l) for i,j in enum: d[j].append(i) print d