I have a list with a large number of words in it: sentence = ['a','list','with','a','lot','of','strings','in','it']
I want to be be able to go through the list and combine pairs of words according to some conditions I have. e.g.
['a','list','with','a','lot','of','strings','in','it'] becomes ['a list','with','a lot','of','strings','in','it']
I have tried something like:
for w in range(len(sentence)):
if sentence[w] == 'a':
sentence[w:w+2]=[' '.join(sentence[w:w+2])]
but it doesn't work because joining the strings, decreases the size of the list and causes an index out of range. Is there a way to do this with iterators and .next() or something?