HOWTO · Python

How to Remove All the Occurrences of an Element From a List in Python

In this article, we will see how we can remove all the occurrence of an element from a List in Python.

On this page

A Python list can store the same value many times, and every occurrence is a distinct item at its own position in the list. remove() deletes only the first match, so clearing every occurrence of a value calls for a different approach. The most direct option is a list comprehension, which builds a new list that keeps only the items you want; filter() offers a predicate-based alternative, and a loop around remove() works when the original list must be mutated in place.

Remove all matching items with a list comprehension

A list comprehension keeps each item that satisfies a condition and collects the survivors into a new list. Run this example:

numbers = [1, 2, 3, 4, 2, 5, 2]
target = 2
filtered = [x for x in numbers if x != target]
print(filtered)
print(numbers)
[1, 3, 4, 5]
[1, 2, 3, 4, 2, 5, 2]

The condition x != target keeps every value that is not 2, so all three copies disappear from filtered while numbers still holds the original data. To replace the original variable rather than keep two lists, assign the result back to it: numbers = [x for x in numbers if x != target]. Other names bound to the old list object still see the unfiltered items, which is usually what you want when the caller owns the list.

If the value never appears in the list, the comprehension still runs and produces a list holding every original item:

numbers = [1, 3, 4, 5]
target = 2
filtered = [x for x in numbers if x != target]
print(filtered)
[1, 3, 4, 5]

This makes the comprehension safe to run without checking membership first.

Remove all matching items with filter()

The filter() function applies a predicate to each item and yields only the items for which the predicate is true. In Python 3 it returns an iterator, not a list, so pass its result to list() when you need a reusable list. The predicate is often a lambda, a small anonymous function, that compares each item with the target:

numbers = [1, 2, 3, 4, 2, 5, 2]
target = 2
filtered = list(filter(lambda x: x != target, numbers))
print(filtered)
[1, 3, 4, 5]

Like the comprehension, filter() builds a new sequence and leaves numbers untouched; a target that never occurs produces a list equal to the input. Reach for filter() when a predicate already exists or should be reused, and prefer the comprehension when the condition is a one-off check such as x != target.

Remove all matching items with remove()

remove() is a list method that deletes only the first item equal to its argument and changes the list in place. One call can never clear duplicates, so repeat it while the value is still present:

numbers = [1, 2, 3, 4, 2, 5, 2, 6]
target = 2
while target in numbers:
    numbers.remove(target)
print(numbers)
[1, 3, 4, 5, 6]

Each iteration scans the list once for target in numbers and again inside remove() to locate the first match, so the loop performs two searches per deleted item. The while condition is also the safeguard: when no match remains, the test is false and the loop stops. Calling remove() with a value that is not in the list raises ValueError, so the membership check — or an equivalent numbers.count(target) guard — is what makes repeated removal safe. When the target was never in the list, the loop body never runs and the list keeps all of its items.

Choose the right approach

  • Need a filtered copy: use a list comprehension. It makes one pass over the list, states the condition inline, and leaves the original untouched.
  • Need to reuse a predicate: use filter() with lambda or a named function. The result is a lazy iterator, so remember the list() call when you need a concrete list.
  • Need in-place mutation: use the while ... remove() loop when other code must observe the change on the same list object. Because each deleted item costs a membership scan plus a removal scan, it does more work than the single-pass comprehension on long lists with many matches.

For typical list sizes the comprehension is the clearest and quickest solution to write; the loop’s extra scans only become noticeable when lists grow large or removals run frequently.