Though a process, I expect to have a succession of lists containing strings. Some of those strings will contrain a substring called 'foo'. At all those locations, I would like to insert another string item 'bar' before that.
So if
list1 = ['alpha', 'beta', 'gamma' , 'Donald-foo' , 'omega', 'foo Obama', 'football']
I would liked to create list2 which is
list2 = ['alpha', 'beta', 'gamma' , 'bar', 'Donald-foo' , 'omega', 'bar', 'foo Obama', 'bar', 'football']
So far I have tried this:
list3 = [i for i,s in enumerate(list1) if 'foo' in s]
this gives me
list3 = [3, 5, 6]
for k in reversed(list3):
list1.insert(k,'bar')
This does the job but feels like a very clunky and painful way to go about it. Is there a more elegant approach?