This is my current snippet:
l = ['i', 'b', 'c', 'a', '3', 'a', '7', 'a', '9', 'k', 'a', '66', 'k', 'd']
new_list = []
a = {}
for i in range(len(l)):
if 'a' == l[i]:
a['plus_line'] = l[i+1]
new_list.append(a)
print(new_list)
I am expecting output like this below:
[{'plus_line': '3'}, {'plus_line': '7'}, {'plus_line': '9'}, {'plus_line': '66'}]
But I am getting:
[{'plus_line': '66'}, {'plus_line': '66'}, {'plus_line': '66'}, {'plus_line': '66'}]
I am trying to achieve when a will be in the list, then the next item of the a will be the value of the dict.
[{'plus_line': '66'}, {'plus_line': '66'}, {'plus_line': '66'}, {'plus_line': '66'}]a = [{'plus_line': y} for x, y in zip(l, l[1:]) if x == 'a'].