Write a program to move zeros in Python

In this article, TipsMake.com will work with you to write a program to move zeros in Python.

Problem : Given an array of random numbers, move all zeros in that array to the end of the array.

Example : Given array {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0}, it becomes {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0} after sorting. The order of all other elements is the same.

Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0}; Output : arr[] = {1, 2, 4, 3, 5, 0, 0, 0}; Input : arr[] = {1, 2, 0, 0, 0, 3, 6}; Output : arr[] = {1, 2, 3, 6, 0, 0, 0};

In this article, TipsMake.com will work with you to write a program to move zeros in Python.

Method 1: Use a loop

Move the array "arr" from left to right. While moving, keep checking for non-zero element in array. Let's set the position of non-zero elements as "count". For each element other than 0 arr[i], set the element to "arr[count]" and increment the value "count". Once the move was completed all non-zero elements were moved to the front and "count" was set as the index of the first zero. Now, what we need to do is run a loop for all zero elements from "count" to the end of the array.

Here is sample code:

# Python3 code to move all zeroes # at the end of array # Function which pushes all # zeros to end of an array. def pushZerosToEnd(arr, n): count = 0 # Count of non-zero elements # Traverse the array. If element # encountered is non-zero, then # replace the element at index # 'count' with this element for i in range(n): if arr[i] != 0: # here count is incremented arr[count] = arr[i] count+=1 # Now all non-zero elements have been # shifted to front and 'count' is set # as index of first 0. Make all # elements 0 from count to end. while count < n: arr[count] = 0 count += 1 # Driver code arr = [1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9] n = len(arr) pushZerosToEnd(arr, n) print("Mảng sau khi di chuyển toàn bộ số 0 về cuối :") print(arr) # This code is contributed by "Abhishek Sharma 44"

Method 2: Partition the array

The approach of this method is quite simple. We will use 0 as the pivot element and whenever we see a non-zero element we will swap it with the pivot element. As a result, non-zero elements will appear at the beginning.

Sample code:

# Python Program to move all zeros to the end A = [5, 6, 0, 4, 6, 0, 9, 0, 8] n = len(A) j = 0 for i in range(n): if A[i] != 0: A[j], A[i] = A[i], A[j] # Partitioning the array j += 1 print(A) # Print the array # This code is contributed by Tapesh(tapeshdua420)

Method 3: Use the method to count the number 0

In this approach we will traverse the entire array and will count the number of zeros present in the array. While counting, we will remove zero from the array.

After completing the above process, we will push back the count of zeros into the array.

Here is sample code:

# Python program to shift all zeros # to right most side of array # without affecting order of non-zero # elements # Given list arr = [5, 6, 0, 4, 6, 0, 9, 0, 8] # Storing all non zero values nonZeroValues = [x for x in arr if x != 0] # Storing all zeroes zeroes = [j for j in arr if j == 0] # Updating the answer arr = nonZeroValues + zeroes # Printing the answer print( "Mảng sau khi di chuyển toàn bộ số 0 về cuối là: " + arr)

TipsMake.com hope that this article will be useful to you!

4 ★ | 1 Vote