Python: Remove elements from list by value

This article will discuss different ways to remove first or all occurrences of an element from a list by value.


Table of Contents

Python: Remove the first occurrence of an element from a list by value

In Python, the list class provides a function remove(value) to delete an element from the list. It accepts a value as an argument and deletes the first occurrence of that value from the list. But if the given value does not exist in the list, then it raises the ValueError.

Now let’s use the remove() function to delete the first occurrence of an element 52 from a list of numbers,

list_of_num = [51, 52, 53, 54, 55, 52, 57, 52, 59]

# Remove first occurrence of 52 from list
list_of_num.remove(52)

print(list_of_num)

Output

[51, 53, 54, 55, 52, 57, 52, 59]

The list had multiple occurrences of 52, but the remove() function deleted the first occurrence only.

Python: Remove element from a list by value if exist

What if we call the remove() function to delete an element that does not exist in a list? In that case, it will give Value Error. For example,

list_of_num = [51, 52, 53, 54, 55, 52, 57, 52, 59]

list_of_num.remove(70)

Output

    list_of_num.remove(70)
ValueError: list.remove(x): x not in list

We are trying to delete an element with value 72 from the list, but the list didn’t have that. Therefore it raised the value error. We should first check if the element exists in the list or not before calling the remove() function. For example,

list_of_num = [51, 52, 53, 54, 55, 52, 57, 52, 59]

elem = 70

if elem in list_of_num:
    list_of_num.remove(elem)

print(list_of_num)

Output:

[51, 52, 53, 54, 55, 52, 57, 52, 59]

Python: Remove all occurrences of an element from a list by value

As we saw in the previous examples, the remove() function always deletes the given element’s first occurrence from the list. To delete all the occurrences of an element, we need to call the remove() function in a loop, until all the occurrences are not deleted. For example,

def remove_all_occurrences(list_obj, value):
    while value in list_obj:
        list_of_num.remove(value)


list_of_num = [51, 52, 52, 55, 55, 52, 57, 52, 55]

remove_all_occurrences(list_of_num, 52)

print(list_of_num)

Output:

[51, 55, 55, 57, 55]

It deleted all the occurrences of 52 from the list of numbers.

Python: Remove all occurrences of multiple elements from a list by values

In the previous example, we deleted all the occurrences of an element from the list. So we can use the same logic to delete all occurrences of multiple elements from the list.

Suppose we have a list of numbers, and we have another list of values that we want to delete for the original list. We want to delete all elements of list2 from list1.

We have created a separate function for the same, it accepts two different lists as arguments,

  • The first one is the list from which we need to delete the elements
  • The second list contains the elements which we want to delete.

For each element of the second list, it deletes all the occurrences from the original list. Let’s understand by an example,

def remove_all_by_values(list_obj, values):
    for value in values:
        while value in list_obj:
            list_of_num.remove(value)


list_of_num = [51, 52, 52, 55, 55, 52, 57, 52, 55, 61, 62]

remove_all_by_values(list_of_num, [52, 55, 57])

print(list_of_num)

Output

[51, 61, 62]

It deleted all the occurrences of 52, 55, and 57 from the list.

Summary

In this article, we learn different ways to delete the first or all occurrences of an element from a list. We also analyzed how to delete multiple elements from a list by value.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top