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!
You should read it
- Python online editor
- More than 100 Python exercises have solutions (sample code)
- How to set up Python to program on WSL
- Bookmark 5 best Python programming learning websites
- For in Python loop
- Manage files and folders in Python
- Write a program to check duplicate values in Python
- Package in Python
- Multiple choice quiz about Python - Part 3
- 5 choose the best Python IDE for you
- What is Python? Why choose Python?
- Module time in Python
Maybe you are interested
6 ways to fix arrow keys not working in Excel
Why should you use Carrot Weather instead of the default Weather app on iPhone?
Video: Close-up of the terrifying power of the 'super rocket' that fired 100 arrows at once in the 15th century
Microsoft removes a barrier preventing users from updating to Windows 11 24H2
Barrier protects London from flooding
What to eat when you have diarrhea?