0

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

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.

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
maverick
  • 53
  • 8

3 Answers3

4

If the function must return None

This is because you reassign list to a new list made with list1 + list2. You need to modify the list you send to your function with the temporary list made with the concatenation of list1 and list2.

def move_zero(lst):
    no_zero = list()
    only_zero = list()
    for i in lst:
        if i != 0:
            no_zero.append(i)
        else:
            only_zero.append(i)

    temp = no_zero + only_zero 
    i = 0
    for e in temp:
        lst[i] = e
        i += 1

    return  # return None is implicit, you can remove this line

a = [0,1,0,2,0,3,0,4]
print(move_zero(a), a)

If you can use enumerate()

    temp = no_zero + only_zero
    for i, e in enumerate(temp):
        lst[i] = e

If you can use list.clear() and list.extend()

    temp = no_zero + only_zero 
    lst.clear()
    lst.extend(temp)
Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
  • 2
    This is the correct answer as everyone else mentioning returning the list which is clearly prohibited in the question. Worth to mention that this mainly is a exercise to learn about mutable types in python so I want to reference this for further reading: https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – hhz Dec 03 '20 at 15:00
0

The issue in your function is that you are not returning lst at the end of your function, which you could then re-assign over the original list.

More concisely, you can just use sorted with a key that checks if the element is zero or not.

def move_zero(lst):
    return sorted(lst, key=lambda i: i == 0)

>>> move_zero([0,1,0,2,0,3,0,4])
[1, 2, 3, 4, 0, 0, 0, 0]
>>> move_zero([0,1,2,0,1])
[1, 2, 1, 0, 0]
>>> move_zero([1,2,3,4,5,6,7,8])
[1, 2, 3, 4, 5, 6, 7, 8]

If you wanted to sort the list in-place instead of making a new one, you could replace the implementation with

def move_zero(lst):
    lst.sort(key=lambda i: i == 0)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0
def moveZeros(arr): 
  
return [nonZero for nonZero in arr if nonZero!=0] + \ 
       [Zero for Zero in arr if Zero==0] 

if __name__ == "__main__": 
    arr = [1, 2, 0, 4, 3, 0, 5, 0] 
    print (moveZeros(arr)) 

Try this it is a better method check out the geeks for geeks article. Below is your code corrected.

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.clear()
  lst = list1 + list2 

lst = [0,1,4,5,0,9,7]
move_zero(lst[:])
print(lst)

You can refer to this link. https://www.python-course.eu/passing_arguments.php. To change the value of list in place please refer to @DorianTurba's answer above