PYTHON PROGRAM- PUSH | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

PYTHON PROGRAM- PUSH

Write a Python program to push all zeros to the end of a given list a. The order of the elements should not change. Input Format: Elements of the list a with each element separated by a space. Output Format: Elements of the modified list with each element separated by a space. After the last element, there should not be any space. Example: Input: 0 2 3 4 6 7 10 Output: 2 3 4 6 7 10 0 Explanation: There is one zero in the list. After pushing it at the end the elements of the list becomes 2 3 4 6 7 10 0. The order of other elements remains the same.

23rd Sep 2018, 4:58 AM
Krishnanshu Dey
Krishnanshu Dey - avatar
3 Answers
+ 2
originalList=[0,1,2,3] newList=[] numOf0=0 for i in originalList: if i is 0: numOf0=numOf0+1 if i is not 0: newList.append(i) for i in range(numOf0): newList.append(0) print(newList)
13th Oct 2018, 5:13 PM
George Ryan
George Ryan - avatar
+ 1
You can try this code l=list(map(int,input().split())) for i in range(len(l)): if l[i] == 0 : l.pop(i) l.append(0) print(*l, end="")
12th Nov 2020, 3:15 PM
sonajosf
sonajosf - avatar
0
Write a Python program to push all zeros to the end of a given list a. The order of the elements should not change. Input Format: Elements of the list a with each element separated by a space. Output Format: Elements of the modified list with each element separated by a space. After the last element, there should not be any space. Example: Input: 0 2 3 4 6 7 10 Output: 2 3 4 6 7 10 0 Explanation: There is one zero in the list. After pushing it at the end the elements of the list becomes 2 3 4 6 7 10 0. The order of other elements remains the same.
2nd Nov 2020, 4:20 AM
Saksham Turki
Saksham Turki - avatar