Program to Move all Zeros present in an ArrayList to the End

Python Program to Move all Zeros present in an Array/List to the End

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have the immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

Given a list the task is to move all zeros present in the given list to the end in python

Examples:

Example1:

Input:

given list = [7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]

Output:

the list before modification :
[7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]
the list after modification :
[7, 2, 34, 56, 12, 5, 6, 8, 9, 1, 2, 4, 5, 0, 0, 0, 0, 0]

Example2:

Input:

given list = [0, 54, 0, 6, 0, 7, 0, 0, 0, 9, 9, 9, 8, 8, 45, 33, 77, 66, 88, 11, 21, 0, 0, 0, 0]

Output:

the list before modification :
[0, 54, 0, 6, 0, 7, 0, 0, 0, 9, 9, 9, 8, 8, 45, 33, 77, 66, 88, 11, 21, 0, 0, 0, 0]
the list after modification :
[54, 6, 7, 9, 9, 9, 8, 8, 45, 33, 77, 66, 88, 11, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Python Program to Move all Zeros present in an Array/List to the End

There are several ways to move all zeros present in the given array/list to the end some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1: Taking New List and using conditional statements

The concept is straightforward, if the current element is non-zero, move it to the next available location in the array. After all of the array’s elements have been processed, increment all remaining indices by 0.

Approach:

  • Create a new list and call it emptylist.
  • Take a count, say zeroCount, and set it to 0 (which counts the total amount of zeros in the given list).
  • Traverse the given array/list using for loop.
  • Using the if condition, determine whether the element is zero or not.
  • If the element is not equal to zero, use the append() function to append it to the emptylist.
  • Otherwise, increase the zeroCount by one.
  • Following the execution of the for loop. Using the append() function, append the 0s to the end of the list zeroCount times.
  • Print the emptylist.

Below is the implementation:

# Create a new list and call it emptylist
emptylist = []
# given list
given_list = [7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]
# print the list before modifictaion
print("the list before modification :")
print(given_list)
# Take a count, say zeroCount, and set it to 0 (which counts the
# total amount of zeros in the given list).
zeroCount = 0
# Traverse the given array/list using for loop.
for eleme in given_list:
    # Using the if condition, determine whether the element is zero or not.
    if(eleme != 0):
        # If the element is not equal to zero, use the
        # append() function to append it to the emptylist.
        emptylist.append(eleme)
    # Otherwise, increase the zeroCount by one.
    else:
        zeroCount = zeroCount+1
# adding the zeros to the end of the empty list zeroCount number of times
for i in range(zeroCount):
    emptylist.append(0)
# print the list after modifictaion
print("the list after modification :")
print(emptylist)

Output:

the list before modification :
[7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]
the list after modification :
[7, 2, 34, 56, 12, 5, 6, 8, 9, 1, 2, 4, 5, 0, 0, 0, 0, 0]

Method #2: Using Quicksort’s partitioning logic

By changing Quicksort’s partitioning algorithm, we can solve this problem in a single scan of the array. The plan is to use 0 as a pivot element and run the partition procedure once. The partitioning logic scans all elements and swaps every non-pivot element with the pivot’s first occurrence.

Approach:

  • Scan the given list or give list input as static
  • Take a count, say index, and set it to 0 (which counts the total amount of zeros in the given list).
    Traverse the given array/list using for loop.
  • Using the if condition, determine whether the element is zero or not.
  • If the element is not equal to 0 then swap the current element with the index element
  • like  given_list[i],given_list[index]=given_list[index],given_list[i]
  • Increment the index
  • Print the given list

Below is the implementation:

# given list
given_list = [7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]
# print the list before modifictaion
print("the list before modification :")
print(given_list)
# Take a count, say zeroCount, and set it to 0
index = 0
# Traverse the given array/list using for loop.
for eleme in range(len(given_list)):
    # Using the if condition, determine whether the element is zero or not.
    if(given_list[eleme] != 0):
        # swappping index element with current index
        given_list[index], given_list[eleme] = given_list[eleme], given_list[index]
        # increment the index value by 1
        index = index+1
# print the list after modifictaion
print("the list after modification :")
print(given_list)

Output:

the list before modification :
[7, 2, 0, 34, 56, 12, 0, 5, 6, 8, 0, 0, 9, 0, 1, 2, 4, 5]
the list after modification :
[7, 2, 34, 56, 12, 5, 6, 8, 9, 1, 2, 4, 5, 0, 0, 0, 0, 0]

Related Programs: