How to Remove Multiple Items From List in Python?

Recently in a Python webinar, someone asked me a question about removing multiple items from the list. After researching and experimenting with various methods I found several effective ways to accomplish this task. In this tutorial, I will explain how to remove multiple items from list in Python with suitable examples.

Remove Multiple Items From List in Python

Before getting into the methods, let’s establish a solid foundation. Python lists are mutable, ordered collections that can store items of different data types. When it comes to removing elements, Python provides several built-in methods, each with its advantages and use cases.

# A simple Python list
sample_list = [10, 20, 30, 40, 50, 60, 70, 80]

Now, let’s explore how to remove multiple items from this list efficiently.

Read How to Reverse a List in Python?

Method 1: Use List Comprehension

Python list comprehension is one of efficient features for its elegance and readability. It’s particularly effective for filtering out multiple items from a list.

Example:

original_list = [10, 20, 30, 40, 50, 60, 70, 80]
items_to_remove = [20, 50, 70]

filtered_list = [item for item in original_list if item not in items_to_remove]
print(filtered_list)

Output:

[10, 30, 40, 60, 80]

I executed the above example code and added the screenshot below.

python remove multiple items from list

When to Use List Comprehension:

  • When you need a new list without modifying the original
  • For readability and concise code
  • When dealing with simple filtering conditions

Check out How to Convert a List to a String in Python?

Method 2: Use the filter() Function

Python filter() function provides a functional programming approach to remove items based on a specified condition.

Example:

original_list = [10, 20, 30, 40, 50, 60, 70, 80]
items_to_remove = [20, 50, 70]

filtered_list = list(filter(lambda x: x not in items_to_remove, original_list))
print(filtered_list)

Output:

[10, 30, 40, 60, 80]

I executed the above example code and added the screenshot below.

remove several elements from list python

When to Use filter():

  • When you prefer a functional programming style
  • When the filtering logic is more complex
  • When you want to maintain readability for other developers familiar with functional patterns

Read How to Remove the Last Element from a List in Python?

Method 3: Use a For Loop with a New List

Sometimes, the most simple approach is using a for loop to build a new filtered list.

Example:

original_list = [10, 20, 30, 40, 50, 60, 70, 80]
items_to_remove = [20, 50, 70]

filtered_list = []
for item in original_list:
    if item not in items_to_remove:
        filtered_list.append(item)

print(filtered_list)

Output:

[10, 30, 40, 60, 80]

I executed the above example code and added the screenshot below.

remove multiple elements from list python

When to Use For Loops:

  • When clarity is more important than conciseness
  • When teaching Python to beginners
  • When you need to perform additional operations during filtering

Check out How to Flatten a List of Lists in Python?

Method 4: In-Place Removal Using a While Loop

If you need to modify the original Python list rather than create a new one, a while loop with careful indexing can be effective.

Example:

original_list = [10, 20, 30, 40, 50, 60, 70, 80]
items_to_remove = [20, 50, 70]

i = 0
while i < len(original_list):
    if original_list[i] in items_to_remove:
        original_list.pop(i)
    else:
        i += 1

print(original_list)  # Output: [10, 30, 40, 60, 80]

When to Use While Loops for In-Place Removal:

  • When you need to modify the original list directly
  • When memory efficiency is a concern
  • When you need precise control over the iteration process

Read How to Sort a List of Tuples in Python?

Method 5: Use the remove() Method in a Loop

Python remove() method can be used to delete specific items from a list. To remove multiple items, we can use it in a loop.

Example:

original_list = [10, 20, 30, 40, 50, 60, 70, 80]
items_to_remove = [20, 50, 70]

# Using a copy to avoid modification during iteration
for item in items_to_remove:
    if item in original_list:
        original_list.remove(item)

print(original_list)  # Output: [10, 30, 40, 60, 80]

The remove() method only removes the first occurrence of the specified value. If you have duplicates, you’ll need additional logic:

original_list = [10, 20, 30, 20, 40, 50, 20, 60, 70, 80]
items_to_remove = [20, 50, 70]

for item in items_to_remove:
    while item in original_list:
        original_list.remove(item)

print(original_list)  # Output: [10, 30, 40, 60, 80]

Check out How to Find the Length of a List in Python?

Method 6: Use Sets for Efficient Filtering

When working with large lists, using sets can significantly improve performance due to their O(1) lookup time.

Example:

original_list = [10, 20, 30, 40, 50, 60, 70, 80]
items_to_remove = [20, 50, 70]

# Convert to sets for O(1) lookup
remove_set = set(items_to_remove)
filtered_list = [item for item in original_list if item not in remove_set]

print(filtered_list)  # Output: [10, 30, 40, 60, 80]

When to Use Sets:

  • When dealing with larger lists where performance matters
  • When the order of elements is not important
  • When you’re removing a significant number of items

Compare Performance of all Methods

I’ve benchmarked these methods with different list sizes. Here’s a performance comparison to help you choose the most appropriate method for your specific use case:

MethodSmall ListsMedium ListsLarge ListsPreserves OrderCreates New List
List ComprehensionVery FastFastModerateYesYes
filter()FastFastModerateYesYes
For LoopModerateModerateSlowYesYes
While Loop (In-place)ModerateSlowVery SlowYesNo
remove() MethodSlowVery SlowExtremely SlowYesNo
Set-based FilteringFastVery FastVery FastYesYes

Read How to Get the Last Element of a List in Python?

Real-World Applications

Let me share a few real-world scenarios where removing multiple items from lists is particularly useful:

Data Cleaning

Outliers are extreme values in a dataset that can cut results and affect data analysis. This example demonstrates removing outliers from a list of temperature readings.

# Removing outliers from a dataset
temperatures = [72, 75, 68, 71, 101, 73, 69, 92, 74, 77]
outliers = [101, 92]  # Values identified as outliers

clean_data = [temp for temp in temperatures if temp not in outliers]
print(clean_data)  # Output: [72, 75, 68, 71, 73, 69, 74, 77]

By removing extreme values, we get a dataset that is more reliable for analysis.

Web Development

When handling user data (e.g., from an API or database), it’s essential to filter out users who are inactive or suspended.

# Filtering out inactive users from a user list
users = [
    {"name": "John", "state": "active"},
    {"name": "Sarah", "state": "inactive"},
    {"name": "Mike", "state": "active"},
    {"name": "Emily", "state": "suspended"},
    {"name": "David", "state": "active"}
]

states_to_filter = ["inactive", "suspended"]
active_users = [user for user in users if user["state"] not in states_to_filter]

# Results in only active users

Ensures that only relevant data is passed to the application.

Check out How to Split a List in Python?

Conclusion

In this tutorial, I have explained how to remove multiple items from list in Python. I discussed six methods such as using list comprehension , using filter() function, using for loop with a new list, while loop, remove() method, and sets for efficient filtering. I also explained comparing performances of all methods and some real-world applications.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.