Last modified: May 30, 2026

Remove All Elements by Value Python

Removing all elements by value from a Python list is a common task. You might need to delete every occurrence of a specific item. This article shows you the best ways to do it.

We cover multiple methods. Each method works differently. Choose the one that fits your needs.

Why Remove All Elements by Value?

Sometimes a list has duplicate values. You want to delete all of them. For example, remove all zeros from a number list. Or remove all "apple" strings from a fruit list.

The standard list.remove() method only removes the first occurrence. To remove all, you need a different approach.

Method 1: Using List Comprehension

List comprehension is the cleanest and fastest method. It creates a new list with only the items you want to keep.

You use a condition to filter out the unwanted value. This method is Pythonic and readable.

 
# Original list with duplicate values
numbers = [1, 2, 3, 2, 4, 2, 5]

# Remove all occurrences of value 2
value_to_remove = 2
new_list = [num for num in numbers if num != value_to_remove]

print(new_list)

[1, 3, 4, 5]

The list comprehension iterates through each element. It keeps only those that are not equal to 2. The result is a new list without any 2s.

This method is fast and easy to read. It works well for most cases.

Method 2: Using the filter() Function

The filter() function is another good option. It applies a condition to each element. It returns an iterator with the matching items.

You can convert the iterator back to a list. This method is slightly more functional in style.

 
# Original list
fruits = ["apple", "banana", "apple", "cherry", "apple"]

# Remove all "apple" values
value_to_remove = "apple"
filtered_list = list(filter(lambda x: x != value_to_remove, fruits))

print(filtered_list)

['banana', 'cherry']

The lambda function checks each item. It returns True for items to keep. The list() converts the filter object to a list.

This method works well. It is a bit more verbose than list comprehension.

Method 3: Using a While Loop

A while loop with list.remove() can remove all occurrences. But it is slower and less efficient. Use this only for small lists or learning purposes.

You loop until the value is no longer in the list. Each iteration removes the first occurrence. This continues until none remain.

 
# Original list
colors = ["red", "blue", "red", "green", "red"]

# Remove all "red" values
value_to_remove = "red"
while value_to_remove in colors:
    colors.remove(value_to_remove)

print(colors)

['blue', 'green']

This method is not recommended for large lists. It has O(n²) time complexity. The in check and remove() both scan the list.

Method 4: Using del with List Comprehension

You can combine del with a loop. But this is tricky. You must iterate backwards to avoid index shifting.

A simpler approach is to use list comprehension as shown earlier. For completeness, here is a backward loop example.

 
# Original list
data = [10, 20, 30, 20, 40, 20]

# Remove all 20 values using del and backward loop
value_to_remove = 20
for i in range(len(data) - 1, -1, -1):
    if data[i] == value_to_remove:
        del data[i]

print(data)

[10, 30, 40]

The loop starts from the end. It deletes matching items safely. This method modifies the original list in place.

List comprehension is usually better. It is cleaner and faster.

Method 5: Using numpy (for Large Datasets)

If you work with large numerical lists, consider numpy. It is a powerful library for array operations. It can remove values efficiently.

First, convert the list to a numpy array. Then use boolean indexing to filter.

 
import numpy as np

# Original list
big_numbers = [5, 10, 15, 10, 20, 10, 25]

# Remove all 10 values
value_to_remove = 10
arr = np.array(big_numbers)
filtered_arr = arr[arr != value_to_remove]

print(filtered_arr.tolist())

[5, 15, 20, 25]

Numpy is fast for large data. It uses optimized C code. But it adds a dependency to your project.

Performance Comparison

For small lists, all methods work fine. For large lists, list comprehension is fastest. The while loop is the slowest.

Use list comprehension for general cases. Use numpy for huge numerical datasets. Use filter if you prefer functional style.

If you need to remove NaN values from a list, check our guide on Remove NaN from Python List. It uses similar techniques.

For removing items by index, see Python List Remove by Index. It is a different operation.

To learn about the del keyword in depth, read Python List del: Remove Items Easily.

Important Considerations

List comprehension creates a new list. It does not modify the original. If you need to keep the original, assign the result to a new variable.

The while loop modifies the original list. This can be useful, but it is slow.

Filter returns an iterator. You must convert it to a list if you need a list object.

Always test your code with sample data. Ensure it removes all occurrences correctly.

Conclusion

Removing all elements by value from a Python list is easy. The best method is list comprehension. It is fast, clean, and Pythonic.

Use filter() for functional style. Use a while loop only for small lists. Use numpy for large numerical data.

Choose the method that fits your project. Practice with the examples above. You will master list manipulation in no time.