How To Remove All Occurrences Of An Element From A List In Python

To remove all occurrences of an element from a List in Python, you can use the filter(), remove() functions or list comprehension. Let’s get started to learn more about them.

Remove All Occurrences Of An Element From A List In Python

Using the list comprehension

You can use the list comprehension to remove all occurrences of an element from a List in Python. Simply, you combine it with the if condition and choose the elements that are not the desired value.

Look at the example below.

def remove_occurrences(my_list=list(), element=0):
    # Remove all occurrences of an element from a List with the list comprehension.
    result = [item for item in my_list if item != element]
    return result

# Apply this function with this list.
my_list = [0, 0, 1, 3, 4, 4, 1, 5, 0, 0, 7, 5]
print('The initial list: {}'.format(my_list))

# Remove 0 to the list.
my_list = remove_occurrences(my_list, 0)
print('The new list: {}'.format(my_list))

Output

The initial list: [0, 0, 1, 3, 4, 4, 1, 5, 0, 0, 7, 5]
The new list: [1, 3, 4, 4, 1, 5, 7, 5]

Using the filter() function

Besides the list comprehension, you can use the filter() function combined with the __ne__() method to remove all occurrences of an element from a List.

Look at the example below.

def remove_occurrences(my_list=list(), element=0):
    # Remove all occurrences of an element from a List with the filter() function.
    result = list(filter((element).__ne__, my_list))
    return result

# Apply this function with this list.
my_list = [0, 0, 1, 3, 4, 4, 1, 5, 0, 0, 7, 5]
print('The initial list: {}'.format(my_list))

# Remove 0 to the list.
my_list = remove_occurrences(my_list, 0)
print('The new list: {}'.format(my_list))

Output

The initial list: [0, 0, 1, 3, 4, 4, 1, 5, 0, 0, 7, 5]
The new list: [1, 3, 4, 4, 1, 5, 7, 5]

Using the remove() function

Moreover, you can use the remove() function to remove all occurrences of an element from a List. Because the remove() function can remove only one value per statement so you can combine it with the try-except method to achieve your goal.

Look at the example below.

def remove_occurrences(my_list=list(), element=0):
    # Remove all occurrences of an element from a List with the remove() function.
    while True:
        try:
            my_list.remove(element)
        except ValueError:
            # Break the while loop if all desired values are removed.
            break
    return my_list

# Apply this function with this list.
my_list = [0, 0, 1, 3, 4, 4, 1, 5, 0, 0, 7, 5]
print('The initial list: {}'.format(my_list))

# Remove 0 to the list.
my_list = remove_occurrences(my_list, 0)
print('The new list: {}'.format(my_list))

Output

The initial list: [0, 0, 1, 3, 4, 4, 1, 5, 0, 0, 7, 5]
The new list: [1, 3, 4, 4, 1, 5, 7, 5]

Summary

We have shown you how to remove all occurrences of an element from a List in Python in 4 ways. From our point of view, you should use the list comprehension because it is popular and easy to use. We hope this tutorial is helpful to you. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *