I am currently learning python and I face a problem, any help would be much appreciated.
Here is the task :
- Given a list of integers, moves all non-zero numbers to the beginning of the list and moves all zeros to the end of the list.
- This function returns nothing and changes the given list itself.
For example:
- After calling
move_zero([0,1,0,2,0,3,0,4])
, the given list should be[1,2,3,4,0,0,0,0]
and the function returns nothing - After calling
move_zero([0,1,2,0,1])
, the given list should be[1,2,1,0,0]
and the function returns nothing - After calling
move_zero([1,2,3,4,5,6,7,8])
, the given list should be[1,2,3,4,5,6,7,8]
and the function returns nothing - After calling
move_zero([])
, the given list should be[]
and the function returns nothing
- After calling
Here is my code :
def move_zero(lst):
list1 = [] #initialise list which will contain != 0 numbers
list2 = [] #initialise list which will contain == 0 numbers
for i in lst:
if i != 0:
list1.append(i)
else :
list2.append(i)
lst = list1 + list2
return
My function doesn't return anything as asked, but when I print(lst)
outside the function, I get the initial list again.
I understand that I am wrongly assigning a variable, but I can't find my mistake.