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.

Input : [0,2,3,4,6,7,10]
Output : [2, 3, 4, 6, 7, 10, 0]

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]))

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:

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]))

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program