• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Remove All Instances of Value from List in Python

Remove All Instances of Value from List in Python

February 14, 2022 Leave a Comment

To remove all instances of a value from a list using Python, the easiest way is to use list comprehension.

list_of_numbers = [1,2,3,4,1,2,1,4,3,2]
list_without_1 = [x for x in list_of_numbers if x != 1]

print(list_without_1)

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

You can also use the Python filter() function.

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

print(list_without_1)

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

When working with lists in Python, 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 all instances of a certain value from a list.

We can easily remove all instances of a value 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 empty strings from a list using list comprehension in Python.

list_of_strings = ["This","","is","a","list","","with","empty","","strings","."]
list_without_empty_strings = [x for x in list_of_strings if x != ""]

print(list_without_empty_strings)

#Output:
['This', 'is', 'a', 'list', 'with', 'empty', 'strings', '.']

Removing All Instances 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 the value you want to get rid of and remove all instances from a list.

Below is some example code showing you how to remove all occurrences of empty strings from a list using the filter() function.

list_of_strings = ["This","","is","a","list","","with","empty","","strings","."]
list_without_empty_strings = list(filter(lambda x: x != "", list_of_strings))

print(list_without_empty_strings)

#Output:
['This', 'is', 'a', 'list', 'with', 'empty', 'strings', '.']

Removing All Instances of Any Value from List Using Python

Above, you’ve seen a few example of how to remove all occurrences of a value from a list in Python.

Below are a few more examples of how to get rid of all occurrences of a value in a list.

For example, if we instead wanted to remove all zeros 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 != 0]

print(list_without_zeros)

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

Another example would be if we have a list of numbers with a lot of NaN values.

Below is an example of how you can remove NaN from a list in Python.

from math import nan, isnan

list_with_nan= [0, 1, 3, nan, nan, 4, 9, nan]
list_without_nan = [x for x in list_with_nan if isnan(x) == False]

print(list_without_nan)

#Output:
[0, 1, 3, 4, 9]

Hopefully this article has been useful for you to learn how to remove all instances of a value from a list in Python.

Other Articles You'll Also Like:

  • 1.  Perfect Numbers in Python
  • 2.  How to Check if String Contains Uppercase Letters in Python
  • 3.  Python turtle write() – Write Text to Turtle Screen
  • 4.  Python Get Operating System Information with os and platform Modules
  • 5.  Using Selenium to Get Text from Element in Python
  • 6.  Get Python Dictionary Keys as List
  • 7.  Using Python to Compare Strings Alphabetically
  • 8.  How to Iterate over Everything in Word Document using python-docx
  • 9.  Divide Each Element in List by Scalar Value with Python
  • 10.  Using Python to Remove Quotes from String

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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