0
0
PythonProgramBeginner · 2 min read

Python Program to Move Zeros to End of Array

You can move zeros to the end of an array in Python by iterating through the list, collecting non-zero elements, and then appending zeros at the end, for example: arr = [x for x in arr if x != 0] + [0] * arr.count(0).
📋

Examples

Input[0, 1, 0, 3, 12]
Output[1, 3, 12, 0, 0]
Input[1, 2, 3, 4]
Output[1, 2, 3, 4]
Input[0, 0, 0]
Output[0, 0, 0]
🧠

How to Think About It

To move zeros to the end, first find all the numbers that are not zero and keep them in order. Then count how many zeros are in the list and add that many zeros at the end. This way, all zeros shift to the end without changing the order of other numbers.
📐

Algorithm

1
Create a new list with all elements from the original list that are not zero.
2
Count how many zeros are in the original list.
3
Add that many zeros to the end of the new list.
4
Return or print the new list.
💻

Code

python
def move_zeros_to_end(arr):
    non_zero = [x for x in arr if x != 0]
    zeros = [0] * arr.count(0)
    return non_zero + zeros

# Example usage
arr = [0, 1, 0, 3, 12]
result = move_zeros_to_end(arr)
print(result)
Output
[1, 3, 12, 0, 0]
🔍

Dry Run

Let's trace the example [0, 1, 0, 3, 12] through the code

1

Extract non-zero elements

non_zero = [1, 3, 12]

2

Count zeros

zeros = [0, 0] because there are 2 zeros

3

Combine lists

result = [1, 3, 12] + [0, 0] = [1, 3, 12, 0, 0]

Stepnon_zero listzeros listResult
1[1, 3, 12]
2[1, 3, 12][0, 0]
3[1, 3, 12, 0, 0]
💡

Why This Works

Step 1: Filter non-zero elements

We use a list comprehension with if x != 0 to keep only the numbers that are not zero, preserving their order.

Step 2: Count zeros

We count how many zeros are in the original list using arr.count(0) to know how many zeros to add at the end.

Step 3: Combine lists

We add the zeros at the end of the non-zero list by concatenating the two lists, moving all zeros to the end.

🔄

Alternative Approaches

In-place swapping
python
def move_zeros_in_place(arr):
    pos = 0
    for i in range(len(arr)):
        if arr[i] != 0:
            arr[pos], arr[i] = arr[i], arr[pos]
            pos += 1
    return arr

arr = [0, 1, 0, 3, 12]
print(move_zeros_in_place(arr))
This method moves zeros in place without extra space but modifies the original list.
Using filter and list concatenation
python
def move_zeros_filter(arr):
    non_zero = list(filter(lambda x: x != 0, arr))
    zeros = [0] * (len(arr) - len(non_zero))
    return non_zero + zeros

arr = [0, 1, 0, 3, 12]
print(move_zeros_filter(arr))
Uses filter function for clarity but similar in performance to list comprehension.

Complexity: O(n) time, O(n) space

Time Complexity

The program loops through the list once to filter non-zero elements and once to count zeros, both operations are O(n).

Space Complexity

It creates new lists for non-zero elements and zeros, so it uses O(n) extra space.

Which Approach is Fastest?

The in-place swapping method is fastest in space as it uses O(1) extra space, but the list comprehension method is simpler and more readable.

ApproachTimeSpaceBest For
List comprehension + countO(n)O(n)Readability and simplicity
In-place swappingO(n)O(1)Memory efficiency and modifying original list
Filter + concatenationO(n)O(n)Functional style and clarity
💡
Use list comprehension to separate non-zero elements and then add zeros at the end for a clean and readable solution.
⚠️
Beginners often try to remove zeros while iterating and modifying the list at the same time, causing errors or skipping elements.