I was hoping someone would be able to help me out with this too. I am trying to solve the same problem, though I am trying to use a for loop & python built in functions as you will see below.
def move_zeros(lst):
new_ls = []
for i in lst:
if i == 0:
new_ls.append(i)
lst.remove(i)
return (lst)
samp_lis = [9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]
print(move_zeros(samp_lis))
However, for what ever reason the for loop only makes it way to index 13 before it concludes. Could someone explain to me why it does this? No matter what I do I can't seem to make the for loop keep going
[9, 0, 0, 9, 1, 2, 0, 1, 0, 1, 0, 3, 0, 1, 9, 0, 0, 0, 0, 9]
Below is the answer I keep getting
[9, 9, 1, 2, 1, 1, 3, 1, 9, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0]
The same answer has been asked before, however, it looks like everyone took a sorting approach. I wanted to know if it was possible to do this with a for loop.
Previous Question: Move all zeros to the end of the array in python