w3resource logo
Python Exercises

Python Challenges: Push all zeros to the end of a list

Python Challenges - 1: Exercise-27 with Solution

Write a Python program to push all zeros to the end of a list.

Explanation:

Python: Push all zeros to the end of a list

Sample Solution:-

Python Code:

def move_zero(num_list):
    a = [0 for i in range(num_list.count(0))]
    x = [ i for i in num_list if i != 0]
    x.extend(a)
    return(x)

print(move_zero([0,2,3,4,6,7,10]))
print(move_zero([10,0,11,12,0,14,17]))

Sample Output:

[2, 3, 4, 6, 7, 10, 0]                                                                                        
[10, 11, 12, 14, 17, 0, 0]

Flowchart:

Python Flowchart: Push all zeros to the end of a list

Python Code Editor:

Improve this sample solution and post your code through Disqus