Remove All the Occurrences of an Element From a List in Python

Sahil Bhosale Jul 18, 2021 Feb 14, 2021
  1. Use the filter() Function to Remove All the Instances of an Element From a List in Python
  2. Use List Comprehension to Remove All the Instances of an Element From a List in Python
  3. Use the remove() Function to Remove All the Instances of an Element From a List in Python
Remove All the Occurrences of an Element From a List in Python

A list in Python allows multiple occurrences of the same element. Even though an element’s value may be the same as others, each element will have a different index. Using these index numbers, you can easily access whichever elements you want.

But there might be some situations where you don’t want multiple instances of the same element; then, you will surely want to remove all of those occurrences of that particular element from a list. In Python, there are different ways in which you achieve this.

Use the filter() Function to Remove All the Instances of an Element From a List in Python

In Python, filtering elements becomes easier with the help of the filter() function. The filter() function takes two arguments, the first argument is a function, and the second argument can be sets, lists, tuples, etc.

Example 1: filter() Function With __ne__

myList = [2, 1, 3, 5, 1, 1, 1, 0]
myList = list(filter((1).__ne__, myList))
print(myList)

Output:

[2, 3, 5, 0]

In this example, you have a list myList from which you want to remove the occurrence of 1. The filter() function takes another function, __ne__, which will return a bool either True or False based on whether the value 1 is present inside the list myList or not. If the value 1 is present inside the list, then it will simply discard it. Then whatever will be returned by the filter() function will be converted to a list using the list() function.

Example 2: filter() Function With lambda Function

myList = [2, 1, 3, 5, 1, 1, 1, 0]
valueToBeRemoved = 1

result = filter(lambda val: val !=  valueToBeRemoved, myList) 
print(list(result)) 

Output:

[2, 3, 5, 0]

Instead of passing __ne__ function, we pass a lambda function to the filter() function.

From the list myList you will take each element one by one and store it inside the val variable. If the elements present inside both val and valueToBeRemoved are not equal, only those present inside val are added to the new variable result. You should convert the result to a list using the list() function.

Use List Comprehension to Remove All the Instances of an Element From a List in Python

A List comprehension is a short way of writing code. List comprehension is faster than the normal functions and loops.

Example code:

myList = [1, 2, 3, 4, 2, 2, 3]
valueToBeRemoved = 2

myList = [value for value in myList if value != valueToBeRemoved]

print(myList)

Output:

[1, 3, 4, 3]

You have a list myList from which you want to remove the occurrence of the element 2. The main code for removing all instances of an element resides inside the square brackets []. Here, the for loop will run first, and then it will take a value from the list myList and store it inside the value variable. After that, if the value inside the value variable and the valueToBeRemoved variable does not match each other, it will only return the value of the value variable and store it in the List myList. This process will continue until the list gets empty. Finally, you will have a list that will contain the desired output which you want.

Use the remove() Function to Remove All the Instances of an Element From a List in Python

The remove() function only removes the first occurrence of the element. If you want to remove all the occurrence of an element using the remove() function, you can use a loop either for loop or while loop.

myList = [2, 1, 3, 5, 1, 1, 1, 0]
valueToBeRemoved = 1
 
try:
    while True:
        myList.remove(valueToBeRemoved)
except ValueError:
    pass
 
print(myList)

Output:

[2, 3, 5, 0]

In the above code, you have created a list myList, and then you have a variable valueToBeRemoved, which will contain the element whose occurrence you want to remove from the list, in this example, 1. While looping through the list, if that element is inside the list, it will be removed from the list using the remove() function.

Sahil Bhosale avatar Sahil Bhosale avatar

Sahil is a full-stack developer who loves to build software. He likes to share his knowledge by writing technical articles and helping clients by working with them as freelance software engineer and technical writer on Upwork.

LinkedIn

Related Article - Python List