I have some code that should find the position of any 0
items in a list and move it to the back whilst preserving the order of the other elements.
def test2(array):
try:
for n in range(len(array)):
array.append(array.pop(array.index(0)))
return array
except ValueError:
return array
This code works fine for any list apart from one with False
in. I think this is because the .index(0)
: will also return the position of any False in the list. Any way to get around this?
For example if array = [0,1,None,2,False,1,0]
then the output should be [1,None,2,False,1,0,0]
With that same input my code produces: [1, None, 2, 1, False, 0, 0]