- 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.
3 Respuestas
+ 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)
+ 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="")
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.