Python List remove()

In this article, we will discuss about the remove() method of List in Python.

In Python, the List class provides a method remove() to delete an element from the list. It accepts an element as an argument and deletes the first occurrence of that element from the list.

Syntax of list remove() method

list.remove(element)

Parameters

element: An item that needs to be deleted from the list.

Returns

Advertisements
  • It does not return anything. Basically the return value is None.

Important point about List remove() method

If we try to erase an element that does not exist in the list. Then the remove() method will raise an ValueError exception.

Let’s see some examples of remove() method of List, to understand it in more detail.

Examples of List remove() method

Suppose we have a list of numbers and we want to delete the first occurrence of number 11 from the list. For that, we will call the remove() function of list and pass 11 as argument. It will delete the first occurrence of 11 from the list.

listOfNumbers = [22, 11, 33, 44, 11, 55, 11, 88, 77]

# Remove first occurrence of given element from List
listOfNumbers.remove(11)

print(listOfNumbers)

Output:

[22, 33, 44, 11, 55, 11, 88, 77]

It deleted the first occurrence of number 11 from the list. Also, the important point to note here is that, it deleted the first occurrence of given element only from the list. There were three entries of number 11 in the list, but it deleted only the first entry of number 11 from the list.

List remove() and ValueError exception

If the given element does not exist in the list then the remove() function will raise the ValueError exception.

For example, we have a list of numbers and when we try to remove the number 99 from the list, the remove() method raises the ValueError exception, because it does not have the number 99 in it.

listOfNumbers = [22, 11, 33, 44, 11, 55, 11, 88, 77]

# Remove first occurrence of given element from List
listOfNumbers.remove(99)

print(listOfNumbers)

Output:

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

Avoid ValueError exception with list remove() method

There are two ways by which we can avoid the ValueError exception while using the list remove() method.

Method 1:

Before calling the remove() function first check if the given element exists in the list or not, using an in operator. If element exists in the list, then only remove the element. Let’s see an example

listOfNumbers = [22, 11, 33, 44, 11, 55, 11, 88, 77]

elem = 99

# check if list contains the element
if elem in listOfNumbers:
    # Remove first occurrence of given element from List
    listOfNumbers.remove(elem)
else:
    print('Element does not exist in the list')

print(listOfNumbers)

Output:

Element does not exist in the list
[22, 11, 33, 44, 11, 55, 11, 88, 77]

First check if the element exists in the list or not. If it exists in the list, then only remove the element.

Method 2:

We can also use try/except while calling the list’s remove() method. If remove() raises the exception, then it will catch that exception, and our application will not crash. Let’s see the example,

listOfNumbers = [22, 11, 33, 44, 11, 55, 11, 88, 77]

elem = 99

try:
    # Remove first occurrence of given element from List
    listOfNumbers.remove(elem)
except:
    print('Element does not exist in the list')

print(listOfNumbers)

Output:

Element does not exist in the list
[22, 11, 33, 44, 11, 55, 11, 88, 77]

Summary

We learned all about the remove() method of List in Python.

Advertisements

Thanks for reading.

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