0
0
PythonProgramBeginner · 2 min read

Python Program to Move All Zeros to End of List

You can move all zeros to the end of a list in Python by using a loop to collect non-zero elements and then appending zeros at the end, like this: result = [x for x in lst if x != 0] + [0] * lst.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 all 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, the order of non-zero numbers stays the same, and all zeros are moved to the end.
📐

Algorithm

1
Get the input list.
2
Create a new list with all elements that are not zero, keeping their order.
3
Count how many zeros are in the original list.
4
Add that many zeros to the end of the new list.
5
Return or print the new list.
💻

Code

python
def move_zeros_to_end(lst):
    non_zeros = [x for x in lst if x != 0]
    zeros_count = lst.count(0)
    return non_zeros + [0] * zeros_count

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

Dry Run

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

1

Filter non-zero elements

non_zeros = [1, 3, 12]

2

Count zeros

zeros_count = 2

3

Combine non-zeros and zeros

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

Stepnon_zeroszeros_countresult
1[1, 3, 12]
22
3[1, 3, 12, 0, 0]
💡

Why This Works

Step 1: Extract non-zero elements

Using list comprehension, we collect all elements that are not zero to keep their original order.

Step 2: Count zeros

We use count(0) to find how many zeros are in the list.

Step 3: Combine lists

We add the zeros at the end by creating a list of zeros multiplied by their count and concatenate it with the non-zero list.

🔄

Alternative Approaches

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

lst = [0, 1, 0, 3, 12]
print(move_zeros_inplace(lst))
This method modifies the list in place without extra space but is slightly more complex.
Using filter and extend
python
def move_zeros_filter(lst):
    non_zeros = list(filter(lambda x: x != 0, lst))
    zeros_count = lst.count(0)
    non_zeros.extend([0] * zeros_count)
    return non_zeros

lst = [0, 1, 0, 3, 12]
print(move_zeros_filter(lst))
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 a new list for non-zero elements and zeros, so space complexity is O(n). The in-place method reduces space to O(1).

Which Approach is Fastest?

The in-place swapping method is fastest in space usage but slightly more complex; list comprehension is simpler and fast enough for most uses.

ApproachTimeSpaceBest For
List comprehension + countO(n)O(n)Readability and simplicity
In-place swappingO(n)O(1)Memory efficiency and large lists
Filter + extendO(n)O(n)Functional style preference
💡
Use list comprehension to separate zeros and non-zeros for a clean and readable solution.
⚠️
Forgetting to count zeros correctly or modifying the list while iterating can cause errors.