HOWTO · Python
How to Fix ValueError: list.remove(x): X Not in List Error in Python
Learn why list.remove(x) raises ValueError and how to remove one or more Python list values safely.
On this page
ValueError: list.remove(x): x not in list means that remove() could not find a value equal to the one you passed. The list may have changed since you checked it, the value may differ from what you expect, or the value may never have been present. list.remove(x) removes by value; use del items[index] or items.pop(index) when you mean to remove an item at a known position.
Why list.remove(x) raises ValueError
remove() accepts one value per call and removes the first matching occurrence. If the list contains duplicates, later matching occurrences remain. A second call for the same value therefore succeeds only if another equal value is still present.
Here is a small successful example. The list is defined in the same snippet so it can be run on its own:
items = ["science", "maths", "computer", "english"]
items.remove("computer")
print(items)
After the first call, "computer" is no longer in items. Calling remove() for it again raises ValueError with the message list.remove(x): x not in list. This runnable example catches the error so the script can show its type and message without terminating:
items = ["science", "maths", "computer", "english"]
items.remove("computer")
try:
items.remove("computer")
except ValueError as error:
print(f"{type(error).__name__}: {error}")
The method searches the current list using equality. For strings, capitalization and whitespace matter: "Computer" and "computer" are different values. For custom objects, equality follows their __eq__ implementation. When an item seems present but removal fails, inspect the list immediately before the call and compare the actual value and type with the value being passed. A prior removal, filtering step, or different input can change the list between those points.
Check whether an item exists before removing it
If the value may be absent, check membership before calling remove(). The in operator also compares by equality, so this guard follows the same matching rule as removal. Define the list and requested value together when adapting the pattern:
items = ["science", "maths", "english"]
item = "computer"
if item in items:
items.remove(item)
print(items)
When the value is present, the guarded call removes its first matching occurrence. When it is absent, the body is skipped and the list stays unchanged. This is useful when absence is an expected condition and no action is needed. If absence indicates a programming mistake, silently skipping it may hide the cause; validate the data or handle the error where the list is built instead.
The membership check and removal are separate operations. In ordinary single-threaded code with no intervening mutation, they describe the same list state. If another part of the program can modify the list between the check and the call, the check alone cannot guarantee that removal will succeed; coordinate access or handle ValueError at the operation that can encounter it.
Remove multiple list items safely
To remove several known-present values, call remove() once for each value. It does not accept a list of values in one call. This standalone example removes two values in sequence:
items = ["science", "maths", "computer", "english"]
items.remove("maths")
items.remove("english")
print(items)
For a collection of requested values, loop over them. Guard each call if any requested value might be missing:
items = ["science", "maths", "computer", "english"]
requested = ["maths", "history", "english"]
for item in requested:
if item in items:
items.remove(item)
print(items)
Without the membership check, a loop can stop with ValueError on the first absent request, and later values will not be processed. The guarded version skips absent requests while continuing through the collection. Both patterns mutate the original list and remove only one occurrence per request. If your goal is instead to remove every item matching a condition, build a new list with a comprehension or filter the list; repeated remove() calls require a deliberate count and matching rule.
Choose the approach that matches the data: use a direct call for a value known to be present, a membership guard when absence is normal, and explicit error handling when absence should be reported. Check the list’s current contents when the error is unexpected.