Sorry if the question wasn't clear enough, I am very new to python. I also apologize in advance if there are any typos in my code.
Say I have a list
list = [a,b,c,a,x,y,b,m,a,z]
And I want to get the index value of the element after each 'a' using a for loop and store it in a dict. (This assumes dict = {} already exists)
for store in list:
if dict.has_key(store) == False:
if list.index(store) != len(list)-1:
dict[store] = []
dict[store].append(list[list.index(store)+1])
else:
if list.index(store) != len(list)-1:
dict[store].append(list[list.index(store)+1])
Now ideally, I would want my dict to be
dict = {'a':['b','x','z'], 'b':['c','m'], 'c':['a']....etc.}
Instead, I get
dict = {'a':['b','b','b'], 'b':['c','c'], 'c':['a']...etc.}
I realized this is because index only finds the first occurrence of variable store. How would I structure my code so that for every value of store I can find the next index of that specific value instead of only the first one?
Also, I want to know how to do this only using a for loop; no recursions or while, etc (if statements are fine obviously).
I apologize again if my question isn't clear or if my code is messy.