A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-list-remove/ below:

Python List remove() Method - GeeksforGeeks

Python List remove() Method

Last Updated : 01 May, 2025

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)

Explanation:

Syntax of remove() method

list_name.remove(obj) 

Parameter:

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)

Explanation:

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:

Related Articles:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4