How to Remove All Instances of an Element from a List in Python

If you’ve called remove() on a Python list and found that duplicates are still sitting there, you’re not doing anything wrong. To remove all instances of an element from a list, use a comprehension: [x for x in items if x != value].

The reason is simple: list.remove() deletes only the first match and then stops. Inside a for loop it even skips items.

In this article, I’ll show you three reliable ways to clear every match, plus the loop trap that leaves some behind. I ran every snippet on Python 3.12.5.

The sample list we’ll clean up

Here’s the data. It’s the shipping city for seven orders placed this week:

orders = ["Austin", "Denver", "Austin", "Austin", "Tampa", "Boise", "Austin"]

Austin shows up four times, two of them back to back, and we want every one gone. If you only need to drop one item by position, see removing the first element from a list.

Method 1: Remove all instances with a list comprehension (Recommended)

This is the method I reach for first. It builds a new list with only the items you want. Here’s the code:

orders = ["Austin", "Denver", "Austin", "Austin", "Tampa", "Boise", "Austin"]

orders = [city for city in orders if city != "Austin"]
print(orders)
print(len(orders), "orders left")

Output:

['Denver', 'Tampa', 'Boise']
3 orders left

That leaves three orders: Denver, Tampa and Boise, in their original order.

Command Prompt output of a Python list comprehension removing every Austin order from a list of seven shipping cities
All four Austin orders are gone, and the remaining cities keep their original order.

How does this code work?

The comprehension visits each city once, and only cities that pass the if test are copied into the new list.

Assigning the result back replaces the old list. list(filter(lambda c: c != "Austin", orders)) gives the same result, but I find the comprehension easier to read. To confirm how many items remain, try checking the size of a list.

Method 2: Remove every match with a while loop and remove()

If you prefer remove(), repeat it until the value is gone. This changes the original list directly. Here’s the code:

orders = ["Austin", "Denver", "Austin", "Austin", "Tampa", "Boise", "Austin"]

while "Austin" in orders:
    orders.remove("Austin")
print(orders)

# missing value: remove() raises, "in" check avoids it
try:
    orders.remove("Austin")
except ValueError as err:
    print("ValueError:", err)

Output:

['Denver', 'Tampa', 'Boise']
ValueError: list.remove(x): x not in list
Command Prompt output of a Python while loop calling remove until no Austin orders are left, then a ValueError for a missing item
The while loop empties out every Austin order, and a later remove() on the missing value raises a ValueError.

How does this code work?

"Austin" in orders is checked before every pass. While it’s true, remove() deletes the first Austin it finds.

So here’s the catch: every pass rescans the list, which gets slow on big lists. Use the comprehension there.

Pro Tip: The in check protects you from the ValueError shown in the last lines. To delete several different values at once, see removing multiple elements from a list.

Method 3: Remove all instances in place with slice assignment

Sometimes other code holds a reference to the same list, and that list itself must change. A plain comprehension would leave it alone. Here’s the difference:

orders = ["Austin", "Denver", "Austin", "Austin", "Tampa", "Boise", "Austin"]

backup = orders                      # a second name for the same list
orders[:] = [city for city in orders if city != "Austin"]
print(orders)
print(backup)                        # same list, so it changed too

Output:

['Denver', 'Tampa', 'Boise']
['Denver', 'Tampa', 'Boise']
Command Prompt output of Python slice assignment removing Austin in place so a second name for the same list also changes
Slice assignment changes the original list, so the second name sees the change too.

How does this code work?

orders[:] = [...] replaces the contents of the existing list instead of making a new one. That’s why backup, which points at the same list, shows the change as well.

Without the [:], the name would point to a new list and backup would keep the old data. It’s the same mutate-or-copy question you meet when merging lists without duplicates.

Why does remove() inside a for loop skip some items?

This trap is behind most “remove all” bugs. Here’s the obvious-looking loop:

orders = ["Austin", "Denver", "Austin", "Austin", "Tampa", "Boise", "Austin"]

for city in orders:
    if city == "Austin":
        orders.remove(city)
print(orders)
print(orders.count("Austin"), "Austin order still there")

Output:

['Denver', 'Tampa', 'Boise', 'Austin']
1 Austin order still there
Command Prompt output of a Python for loop calling remove on a list, leaving one Austin order behind
The for loop with remove() leaves one Austin order in the list instead of removing all four.

How does this code work?

When remove() deletes an item, everything after it shifts left one position. The loop then moves to the next index, so the item that slid into the old spot is never checked.

That’s why back-to-back Austin entries survive. Loop over a copy (for city in orders[:]) or use one of the methods above. A refresher on loops is in for i in range in Python.

Picking the right way to remove all instances

MethodChanges original?Use it when
List comprehensionNo, builds a new listAlmost always; clear and fast
while + remove()YesShort lists, and you like remove()
Slice assignment lst[:] = ...YesOther code shares the same list

Things to Keep in Mind When Removing Items from a List

  • remove() deletes only the first match. Repeat it in a while loop, or filter with a comprehension.
  • Never remove items from a list while looping over that same list. Loop over a copy or build a new list instead.
  • remove() raises a ValueError for a missing value, so check with in first.

When your list needs more trimming, these pair well with this one:

Frequently asked questions

How do I remove all instances of an element from a list in Python?

Use items = [x for x in items if x != value]. It removes every match and keeps the order.

Does list.remove() remove all occurrences?

No, only the first match. Repeat it in a while loop or use a comprehension.

Why does my for loop skip items when I remove from a list?

Removing shifts later items left, so the loop skips one. Loop over a copy instead.

How do I remove all occurrences without creating a new list?

Use slice assignment: items[:] = [x for x in items if x != value]. It changes the existing list in place.

What happens if the element isn’t in the list?

A comprehension returns the same items. remove() raises a ValueError.

Where are list methods documented?

See More on Lists in the Python docs.