w3resource

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:

Contribute your code and comments through Disqus.

Previous: Write a Python program to check if a given string is an anagram of another given string.
Next: Write a Python program to the push the first number to the end of a list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/challenges/1/python-challenges-1-exercise-27.php