Remove all occurrences of an element from a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove all occurrences of an element from a List in Python #

To remove all occurrences of an element from a list:

  1. Use a list comprehension to iterate over the list.
  2. Check if each item is not equal to the value to be removed.
  3. The new list won't contain the specified value.
main.py
# ✅ Remove all occurrences of element from list (list comprehension) my_list = ['one', 'two', 'one', 'three', 'one'] new_list = [item for item in my_list if item != 'one'] print(new_list) # 👉️ ['two', 'three'] # 👇️ if you need to mutate the original list my_list[:] = [item for item in my_list if item != 'one'] print(my_list) # 👉️ ['two', 'three'] # -------------------------------------------------------- # ✅ Remove all occurrences of element from list (for loop) my_list = ['one', 'two', 'one', 'three', 'one'] for item in my_list.copy(): if item == 'one': my_list.remove(item) print(my_list) # 👉️ ['two', 'three']

The first example uses a list comprehension to remove all occurrences of a value from a list.

main.py
my_list = ['one', 'two', 'one', 'three', 'one'] new_list = [item for item in my_list if item != 'one'] print(new_list) # 👉️ ['two', 'three']
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current item is not equal to the string one and return the result.

The new list will only contain values that meet the condition.

The list comprehension doesn't mutate the original list, it returns a new list.

If you need to mutate the original list, use list slicing.

main.py
my_list = ['one', 'two', 'one', 'three', 'one'] my_list[:] = [item for item in my_list if item != 'one'] print(my_list) # 👉️ ['two', 'three']

We used the my_list[:] syntax to get a slice that represents the entire list, so we can assign to the variable directly.

The slice my_list[:] represents the entire list, so when we use it on the left-hand side, we are assigning to the entire list.

This approach removes all occurrences of the specified value from the original list.

Alternatively, you can use a for loop.

Remove all occurrences of an element from List using for loop #

To remove all occurrences of an element from a list:

  1. Use a for loop to iterate over a copy of the list.
  2. On each iteration, check if the current item is equal to the value to be removed.
  3. Use the list.remove() method to remove the matching items.
main.py
my_list = ['one', 'two', 'one', 'three', 'one'] for item in my_list.copy(): if item == 'one': my_list.remove(item) print(my_list) # 👉️ ['two', 'three']

The list.copy method returns a shallow copy of the object on which the method was called.

This is necessary because we aren't allowed to remove items from a list while iterating over it.

However, we can iterate over a copy of the list and remove items from the original list.

main.py
my_list = ['one', 'two', 'one', 'three', 'one'] for item in my_list.copy(): if item == 'one': my_list.remove(item) print(my_list) # 👉️ ['two', 'three']
On each iteration, we check if the current item is equal to the value to be removed and use the list.remove() method to remove the matching elements.

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

The remove() method mutates the original list and returns None.

The most important thing to note when removing items from a list in a for loop is to use the list.copy() method to iterate over a copy of the list.

If you try to iterate over the original list and remove items from it, you might run into difficult to locate bugs.

Remove all occurrences of an element from List using filter() #

To remove all occurrences of an element from a list:

  1. Use the filter() function to filter out the specified value from the list.
  2. Use the list() class to convert the filter object to a list.
  3. The new list won't contain the specified value.
main.py
my_list = ['one', 'two', 'one', 'three', 'one'] new_list = list(filter(lambda x: x != 'one', my_list)) print(new_list) # 👉️ ['two', 'three']

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

The lambda function gets called with each item of the list, checks if the item is not equal to the string one and returns the result.

The filter object only contains values that meet the condition.

The last step is to use the list() class to convert the filter object to a list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.