Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

How to Remove Multiple Items From a List in Python

Use a list comprehension to filter out every occurrence of one or more values in Python. For index-based deletion, use del or pop() and remove separate positions from highest to lowest.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To remove every occurrence of several values, filter the list with a comprehension:

items = [1, 2, 3, 2, 4, 5]
unwanted = {2, 4}
items = [value for value in items if value not in unwanted]

print(items)  # [1, 3, 5]

This creates a new list, removes all matches, and preserves the order of the values that remain. If you mean specific positions rather than values, use index deletion instead.

Choose whether you are removing values or positions

“Remove these items” can mean either remove every element equal to certain values, or delete elements at particular indexes. The right operation depends on which you mean:

What you know Pattern What it does
Values to exclude [x for x in items if x not in unwanted] Creates a filtered list, excluding every matching occurrence.
A condition for keeping items [x for x in items if keep(x)] Creates a list containing items for which the condition is true.
A contiguous range of indexes del items[start:stop] Deletes the slice; the stop index is excluded.
A few separate indexes Delete indexes from highest to lowest Removes those positions without shifting the positions still to be deleted.
One value, first match only items.remove(value) Deletes the first equal item; raises ValueError if none exists.
An index, and you need the deleted item removed = items.pop(index) Deletes and returns that item.

The Python tutorial documents list comprehensions, remove(), pop(), and del for items and slices in its data structures guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Remove all occurrences of one or more values

Use a comprehension with an exclusion test. To remove one value, compare each item with it:

items = ["red", "blue", "red", "green"]
items = [color for color in items if color != "red"]

print(items)  # ['blue', 'green']

To exclude several values, test membership in a collection of unwanted values:

items = ["red", "blue", "red", "green", "blue"]
unwanted = {"red", "blue"}
items = [color for color in items if color not in unwanted]

print(items)  # ['green']

Every matching occurrence is filtered out, including duplicates. The retained items stay in their original relative order. The comprehension returns a new list; it does not alter the original list object.

Keep the same list object

If other parts of your program hold a reference to the existing list and need to see the updated contents through that reference, assign the filtered result to the full slice:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
items = [1, 2, 3, 2, 4]
reference = items
unwanted = {2, 4}

items[:] = [value for value in items if value not in unwanted]

print(reference)  # [1, 3]

This replaces the contents of the existing list rather than rebinding items to a different list.

Remove items that match a condition

When the rule is more complex than membership in a set of values, express the rule as a predicate and keep only the elements that pass it:

numbers = [3, 8, 11, 14, 19]
numbers = [number for number in numbers if number % 2 != 0]

print(numbers)  # [3, 11, 19]

If you already have a named predicate, filter() is another option:

def keep_odd(number):
    return number % 2 != 0

numbers = list(filter(keep_odd, [3, 8, 11, 14, 19]))

In Python 3, filter() produces an iterator, so wrap it in list() when you need a list immediately. The official Functional Programming HOWTO shows filter() and a list comprehension as equivalent ways to apply a predicate. For a short condition, the comprehension makes the keep rule visible alongside the output list.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Delete by index

Delete a contiguous range

Use del with a slice when the positions are next to one another. The start is included and the stop is excluded:

items = ["a", "b", "c", "d", "e"]
del items[1:4]

print(items)  # ['a', 'e']

Delete separate known indexes

When indexes refer to positions in the original list, delete them in descending order. Removing a higher index does not change the position of a lower one that remains to be removed:

items = ["a", "b", "c", "d", "e"]
indexes_to_remove = [1, 3]

for index in sorted(indexes_to_remove, reverse=True):
    del items[index]

print(items)  # ['a', 'c', 'e']

Deleting index 3 first leaves index 1 in place. If you delete index 1 first, the elements after it shift left, and the original index 3 no longer points to the original fourth item.

Use pop() when you need the removed item

pop(index) deletes the item at that position and returns it. It raises IndexError if the index is out of range. With no argument, pop() removes and returns the final item:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
items = ["a", "b", "c"]
removed = items.pop(1)

print(removed)  # 'b'
print(items)    # ['a', 'c']
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why remove() may delete only one item

items.remove(value) removes only the first item equal to value. Calling it once does not remove duplicates, and it raises ValueError if the value is absent. For example:

items = [2, 1, 2, 3]
items.remove(2)

print(items)  # [1, 2, 3]

Use a comprehension such as [x for x in items if x != 2] when you want to remove every occurrence.

Avoid deleting from the list you are iterating over

Deleting an element shifts later elements left. If you iterate forward over the same list while deleting from it, the next element can move into the position just visited and be skipped. A comprehension avoids that mutation-while-iterating problem by building the filtered result rather than deleting entries during the traversal:

items = [1, 2, 2, 3, 2]
items = [value for value in items if value != 2]

print(items)  # [1, 3]

Performance considerations

A comprehension checks the input items and constructs a result list. Repeated in-place deletions can shift later elements, but there is no universal timing claim that applies to every list size, Python implementation, or deletion pattern. If performance is important, benchmark with the actual data, runtime, and distribution of items to remove.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.