Move All Zeroes to End of Array using List Comprehension in Python Last Updated : 22 Jan, 2025 Comments Improve Suggest changes 5 Likes Like Report We will solve this problem in python using List Comprehension in a single line of code. This allows us to create a new list by iterating over an existing list in a concise and efficient manner. We can utilize list comprehension to separate the non-zero elements and zeros, then combine them together to achieve the desired result.We have existing solution for this problem please refer Move all zeroes to end of array. For example, consider the array [0, 1, 9, 0, 5, 0, 4]. The goal is to move all the zeroes to the end of the array, while keeping the order of the non-zero elements intact. The result of this operation would be [1, 9, 5, 4, 0, 0, 0], where the zeroes are shifted to the end and the relative order of 1, 9, 5, 4 is preserved. Python li = [1, 2, 0, 4, 3, 0, 5, 0] # to get non-zero elements first and then append zeros li = [nonZero for nonZero in li if nonZero != 0] + [zero for zero in li if zero == 0] print(li) Output[1, 2, 4, 3, 5, 0, 0, 0] Explanation:[nonZero for nonZero in arr if nonZero != 0] collects all non-zero elements from li .[zero for zero in arr if zero == 0] collects all zeros from li .+ operator concatenates these two lists, resulting in non-zero elements followed by zeros. Create Quiz Comment S Shashank Mishra 5 Improve S Shashank Mishra 5 Improve Article Tags : Python python-list Python list-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like