Python List remove()


Python List remove() method

Python List remove() method removes the first occurrence of specified item from the list.

https://youtu.be/44PA9lXy64Y?si=1EtCx_JhzC3DmKwt

In this tutorial, you will learn the syntax of, and how to use List remove() method, with examples.

Syntax of List remove()

The syntax of List remove() method is

list.remove(value)

You can read the above expression as: In the list, remove the first occurrence of specified value.

Parameters

remove() method can take one parameter. Let us see the parameter, and its description.

ParameterDescription
valueRequired
The value which is searched in the list, and then removed. Only the first occurrence of the value is removed from the list. If there are any more occurrences, those remain in the list.

Return value

remove() method returns the removed item.

Exceptions

remove() method raises ValueError exception if the specified value is not present in the list.

Please note that the remove() method modifies the original list.

Examples

1. Remove item = 8 from the list in Python

In the following program, we take a list my_list with some initial values. We have to remove the item with value 8 in this list.

Call remove() method on the list my_list, and pass the value 8 as argument to the method.

Python Program

my_list = [2, 4, 6, 8, 10, 12]
my_list.remove(8)
print(my_list)

Output

[2, 4, 6, 10, 12]

The first item with the value 8 is removed from the list.

Explanation

[2, 4, 6, 8, 10, 12]   ← given list
          ↑
     value = 8         ← remove first item with this value
[2, 4, 6,    10, 12]   ← resulting list

2. Remove item from list, when item occurs more than once in the list, in Python

Now, we shall take the my_list, with initial values such that the value 8 occurs more than once.

Call remove() method on the list my_list, and pass the value 8 as argument to the method. The remove() method shall remove only the first item with the given value.

Python Program

my_list = [2, 4, 6, 8, 10, 8, 8, 12]
my_list.remove(8)
print(my_list)

Output

[2, 4, 6, 10, 8, 8, 12]

The first item with the specified value 8 is removed from the list.

Explanation

[2, 4, 6, 8, 10, 8, 8, 12]     ← given list
          ↑
     value = 8         ← remove first item with this value
[2, 4, 6,    10, 8, 8, 12]   ← resulting list

3. remove() method when specified value is not in the list

We already mentioned in the Syntax section that remove() method raises ValueError exception if the specified value is not in the list.

In the following program, we shall take a list my_list, with some initial values, and try to remove the item, say 99, that is not in the list.

Python Program

my_list = [2, 4, 6, 8, 10, 12]
my_list.remove(99)
print(my_list)

Output

Traceback (most recent call last):
  File "/Users/pythonexamplesorg/main.py", line 2, in <module>
    my_list.remove(99)
ValueError: list.remove(x): x not in list

Since the specified value is not in the list my_list, the remove() method raises ValueError exception.

Summary

In this tutorial of Python Examples, we learned about List remove() method, how to use remove() method to remove the first item with given value from the list, with syntax and examples.