I'm doing an exercise that asks to move all the zeroes to the end of a list, tried several solutions for practice, all worked, but the one I thought was the most pythonic one using .extend()
doesn't work and returns None
.
This works:
my_list = [0, 0, 1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9, 0]
def move_zeros(my_list):
l = [x for x in my_list if x != 0]
z = [y for y in my_list if y == 0]
l.extend(z)
return l
print(move_zeros(my_list))
This also works:
def move_zeros(my_list):
l = [x for x in my_list if x != 0]
l.extend([y for y in my_list if y == 0])
return l
print(move_zeros(my_list))
But this doesn't work, returns None
:
def move_zeros(my_list):
l = [x for x in my_list if x != 0].extend([y for y in my_list if y == 0])
return l
print(move_zeros(my_list))