Open In App

Python List remove() Method

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Python list remove() function removes the first occurrence of a given item from list. It make changes to the current list. It only takes one argument, element we want to remove and if that element is not present in the list, it gives ValueError.

Example:

Python
a = ['a', 'b', 'c']
a.remove("b")
print(a)

Output
['a', 'c']

Explanation:

  • remove() removes the first occurrence of the specified element from the list.
  • In the example, “b” is removed from the list [‘a’, ‘b’, ‘c’], resulting in [‘a’, ‘c’].

Syntax of remove() method

list_name.remove(obj) 

Parameter:

  • obj: object to be removed from the list 

Return Type: The method does not return any value but removes the given object from the list.

Exception: If the element doesn’t exist, it throws ValueError: list.remove(x): x not in list exception.

Examples of remove() method

1. Passing list as argument in remove()

If we want to remove multiple elements from a list, we can do this by iterating through the second list and using the remove() method for each of its elements.

Python
a = [1, 2, 3, 4, 5, 6, 7, 8]
b = [2, 4, 6]

for item in b:
    if item in a:
        a.remove(item)

print(a)

Output
[1, 3, 5, 7, 8]

Explanation:

  • The code loops through each element in list b and removes the corresponding elements from list a.
  • remove() only removes the first occurrence of the specified element from the list.
  • If an element from b doesn’t exist in a, it is skipped.

2. Trying to delete Element that doesn’t Exist

In this example, we are removing the element  ‘e’ which does not exist in the list which will result in a ValueError. We can use a try except block to handle these exceptions.

Python
a = [ 'a', 'b', 'c', 'd' ]

a.remove('e') 
print(a)

Output:

Traceback (most recent call last):
File “Solution.py”, line 4, in <module>
a.remove(‘e’)
ValueError: list.remove(x): x not in list

Explanation:

  • remove() method raises a ValueError if the specified element is not found in the list.
  • The error message is: “list.remove(x): x not in list”.

Related Articles:


Next Article

Similar Reads