50. Problem to move all zero digits to the end.

In this program, we will take a user input as a list and move all zero digits to the end of a given list of numbers with the help of the below-given steps.

Move all zero digits to the end:

Steps to solve the program
  1. Take a list as input.
  2. Create a variable var and assign its value equal to 0.
  3. Use for loop with the range function to iterate over elements in the list.
  4. If the iterated element is greater than 0 then assign its value to the temp variable.
  5. Assign the value of that index number equal to list1[var]
  6. Now change the value of list1[var] to temp variable.
  7. Add 1 to the var variable.
  8. Print the output to see the result i.e. move all zero digits to the end.
				
					#Input list
list1 = [3, 4, 0, 0, 0, 0, 6, 0, 4, 0, 22, 0, 0, 3, 21, 0]
var = 0

for i in range(0,len(list1)) :
    if (list1[i] > 0) :
        temp = list1[i]
        list1[i] = list1[var]
        list1[var]= temp
        var += 1

#Printing output
print(list1)
				
			

Output :

				
					[3, 4, 6, 4, 22, 3, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0]
				
			

Related Articles

Move all positive numbers on the left

List of lists whose sum of elements is highest

Leave a Comment