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:
- Iterate through the items of the list and use
remove()
method when the item's value matches the item of our interest. - Filter the List with a lambda function having a condition that the item should not match the item of our interest.
- 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
- The
for
loop iterates through each element ofmylist
. - If an element matches
r_item
(value21
), theremove()
method is called to remove it. - This process continues until all occurrences of
21
are removed. - 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
- The
filter()
function creates a new iterable that includes only elements not equal tor_item
. (r_item).__ne__
is a method that checks for inequality withr_item
.- 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
- The
while
loop checks ifr_item
is present inmylist
. - If
r_item
is found, it is removed using theremove()
method. - 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
- A list comprehension iterates through
mylist
, including only elements not equal tor_item
. - This method creates a new list without modifying the original list in place.
- 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.