• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / How to Use Python to Remove Zeros from List

How to Use Python to Remove Zeros from List

February 9, 2022 Leave a Comment

To remove zeros from a list using Python, the easiest way is to use list comprehension.

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]
list_without_zeros = [x for x in list_of_numbers if x != 0]

print(list_without_zeros)

#Output:
[1,4,2,-4,3,-1]

You can also use the Python filter() function.

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]
list_without_zeros = list(filter(lambda x: x != 0, list_of_numbers))

print(list_without_zeros)

#Output:
[1,4,2,-4,3,-1]

When working with lists of numbers, it can be valuable to be able to easily filter and remove unwanted values from your list.

One such situation where you may want to remove values from a list is if you have a lot of zeros in your list.

We can easily remove all zeros from a list using Python with list comprehension. List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

Below is the code which will allow you to remove all zeros from a list using list comprehension in Python.

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]
list_without_zeros = [x for x in list_of_numbers if x != 0]

print(list_without_zeros)

#Output:
[1,4,2,-4,3,-1]

Removing Zeros from List with Python filter() Function

The Python filter() function is a built-in function that allows you to process an iterable and extract items that satisfy a given condition.

We can use the Python filter() function to extract all the items in a list of numbers which do not equal 0 and remove the zeros from a list.

Below is some example code showing you how to remove zeros from a list using the filter() function.

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]
list_without_zeros = list(filter(lambda x: x != 0, list_of_numbers))

print(list_without_zeros)

#Output:
[1,4,2,-4,3,-1]

Removing Any Value from List Using Python

In a very similar way, we can remove any value from a list using list comprehension.

For example, if we instead wanted to remove all of the ones from a list, we could do that easily with list comprehension in Python by adjusting the code above.

list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]
list_without_zeros = [x for x in list_of_numbers if x != 1]

print(list_without_zeros)

#Output:
[0,4,2,-4,0,0,3,0,-1,0]

Another example would be if we have a list of strings, and we want to remove the word “whoa”, we can do that with list comprehension in Python.

list_of_strings = ["whoa","there","hey","there","whoa"]
filtered_list = [x for x in list_of_strings if x != "whoa"]

print(filtered_list)

#Output:
["there","hey","there"]

Hopefully this article has been useful for you to learn how to remove zeros from a list in Python.

Other Articles You'll Also Like:

  • 1.  How to Create Array from 1 to n in Python
  • 2.  pandas set_value – Using at() Function to Set a Value in DataFrame
  • 3.  Remove Empty Strings from List in Python
  • 4.  Python math.factorial() function – Calculate Factorial of Number
  • 5.  Python cube root – Find Cube Root of Number With math.pow() Function
  • 6.  Convert String to Boolean Value in Python
  • 7.  Python dirname – Get Directory Name of Path using os.path.dirname()
  • 8.  Using Python to Find Maximum Value in List
  • 9.  Get pandas Series Last Element in Python
  • 10.  Python Get Number of Cores Using os cpu_count() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy