I need to be able to add an item into list a from list b. The item from list b is to be added as soon as ' ' which is a double space is identified.
Therefore if the first item in the list is not a double space, then the loop goes on to check the next item in the list, if its also not a double space, it carries on until if finds the double space and then it replaces the first available double space with the item from list b. This should be looped so that if I run the function again, an item in list b is popped and added to the next available double space in list a.
a = ['a','c','e','j','h',' ',' ',' ',' ']
b = ['b','d','f','i','g']
x = 4
for item in a:
if item == a[4]:
break
if a[x] != ' ':
a[x+1] = b.pop(-2)
else:
a[x] = a[x+1]
print("list a: ",a)
print("List b: ",b)
Output:
list a: ['a', 'c', 'e', 'j', 'h', 'i', ' ', ' ', ' ']
List b: ['b', 'd', 'f', 'g']
That works, but I have a feeling my code doesn't work on all inputs. Does it? If it doesn't, what's wrong?
x=4isiandireallymovedfrom b to the first available place inlist a