list.remove()
Removes the first occurrence of a value from a list (mutates).
my_list.remove(value)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Removes the first occurrence of a value from a list (mutates).
The list.remove() method performs an in-place modification of a list by deleting the first encountered element that matches the provided value based on equality (__eq__). It is important to note that this is not an identity check (is), but a value comparison. Because the operation requires a linear scan starting from index 0 to find the match, the time complexity is O(n), where n is the length of the list. This makes it inefficient for large datasets or for use within nested loops. The method returns None; it does not return the list or the removed item. If the specified value is not present in the list, a ValueError exception is raised, necessitating defensive programming such as membership testing or exception handling blocks.
Quick reference
Syntax
my_list.remove(value)
Inputs
Parameters
See it in practice
Examples
Removing a Specific String
programming_languages = ['Python', 'Java', 'C++', 'Ruby']
programming_languages.remove('Java')
print(programming_languages)['Python', 'C++', 'Ruby']
The first occurrence of 'Java' is found and removed, shifting subsequent elements to the left.
Handling Duplicate Values
numbers = [10, 20, 30, 20, 40]
numbers.remove(20)
print(numbers)[10, 30, 20, 40]
Only the first occurrence of 20 (at index 1) is removed. The second occurrence remains in the list.
Safe Removal with Membership Check
items = ['keyboard', 'mouse', 'monitor']
target = 'webcam'
if target in items:
items.remove(target)
else:
print(f'{target} not found in list')
print(items)webcam not found in list ['keyboard', 'mouse', 'monitor']
Using the 'in' operator prevents a ValueError when the item might not exist in the collection.
Debug faster
Common Errors
ValueError
Cause: Attempting to remove a value that is not present in the list.
Fix: Check if the item exists using the 'in' keyword before removal, or wrap the call in a try-except block.
data = [1, 2, 3]
data.remove(4) # Raises ValueError: list.remove(x): x not in listLogicError
Cause: Removing items from a list while iterating over it with a for loop, which causes the internal index to skip items.
Fix: Iterate over a shallow copy of the list or use a list comprehension to create a new filtered list.
nums = [1, 2, 3, 4]
for n in nums:
nums.remove(n)
# Result is [2, 4], not [] because the iterator skips indices after removalRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Removes the first occurrence of a value from a list (mutates).
value: Value to remove (first occurrence).
ValueError: Check if the item exists using the 'in' keyword before removal, or wrap the call in a try-except block. LogicError: Iterate over a shallow copy of the list or use a list comprehension to create a new filtered list.