Python List - Remove All Occurrences of an Item or Element


Python List - Remove all occurrences of a specific item

At times, when you are working with Python Lists, you may need to remove the items with a specific value. In this tutorial, we shall learn how to remove all of the items, that have a given specific value, from a list.

There are many ways to remove all the items with a specific value from the List. Following are some of them, which we shall discuss in this tutorial:

  1. Iterate through the items of the list and use remove() method when the item's value matches the item of our interest.
  2. Filter the List with a lambda function having a condition that the item should not match the item of our interest.
  3. Iterate over items while the item is in the list and use remove() method.

The second method is preferred as it gives better performance. The other two methods are for learning purposes.


Examples

1. Remove all occurrences of a specific item in the list using For loop

In the following example, we iterate through each item in the list, using Python For Loop, and when we find a match for the item to be removed, we call remove() function on the list.

Python Program

mylist = [21, 5, 8, 52, 21, 87]
r_item = 21

# Remove the item for all its occurrences
for item in mylist:
    if item == r_item:
        mylist.remove(r_item)

print(mylist)

Explanation

  1. The for loop iterates through each element of mylist.
  2. If an element matches r_item (value 21), the remove() method is called to remove it.
  3. This process continues until all occurrences of 21 are removed.
  4. However, this approach may skip some elements if consecutive elements match r_item.

Output

[5, 8, 52, 87]

2. Remove all occurrences of a specific item in the list using Filter

We filter those items of the list which are not equal to r_item.

Python Program

mylist = [21, 5, 8, 52, 21, 87]
r_item = 21

# Remove the item for all its occurrences
mylist = list(filter((r_item).__ne__, mylist))

print(mylist)

Explanation

  1. The filter() function creates a new iterable that includes only elements not equal to r_item.
  2. (r_item).__ne__ is a method that checks for inequality with r_item.
  3. All occurrences of 21 are removed in one step, producing an updated list.

Output

[5, 8, 52, 87]

3. Remove all occurrences of a specific item in the list using While loop

While there is a match with an item in the list, call remove() function on the list.

Python Program

mylist = [21, 5, 8, 52, 21, 87]
r_item = 21

# Remove the item for all its occurrences
while r_item in mylist:
    mylist.remove(r_item)

print(mylist)

Explanation

  1. The while loop checks if r_item is present in mylist.
  2. If r_item is found, it is removed using the remove() method.
  3. The loop continues until all occurrences of 21 are removed from the list.

Output

[5, 8, 52, 87]

4. Remove all occurrences using List Comprehension

List comprehension can be used to create a new list with elements that do not match r_item.

Python Program

mylist = [21, 5, 8, 52, 21, 87]
r_item = 21

# Remove the item for all its occurrences
mylist = [item for item in mylist if item != r_item]

print(mylist)

Explanation

  1. A list comprehension iterates through mylist, including only elements not equal to r_item.
  2. This method creates a new list without modifying the original list in place.
  3. All occurrences of 21 are removed efficiently.

Output

[5, 8, 52, 87]

Summary

In this tutorial of Python Examples, we learned how to remove all occurrences of an item or element from the list using different approaches like for loop, filter(), while loop, and list comprehension.




Python Libraries