ValueError: list.remove(x): x not in list
1 · The lesson
readWhat this error means
You asked a list to find a value, and the value isn't in the list. Two list methods search like this, and both raise ValueError when they find nothing: .remove(x) deletes the first item equal to x, and .index(x) tells you the position of the first item equal to x. "Equal" is strict here: "Blue" is not "blue", and the text "2" is not the number 2. So when you're sure the value is in the list, check what is really in it.
When you see it
From .remove(). The message always says x, whatever the value was:
Traceback (most recent call last): File "main.py", line 2, in <module> colors.remove("blue") ~~~~~~~~~~~~~^^^^^^^^ ValueError: list.remove(x): x not in list
From .index(). This message shows the value it looked for:
Traceback (most recent call last): File "main.py", line 2, in <module> print(colors.index("blue")) ~~~~~~~~~~~~^^^^^^^^ ValueError: 'blue' is not in list
Why it happens
1. The value really isn't there. It was never added, or it was already removed. .remove() only deletes one copy, so calling it twice for a value that is in the list once fails the second time.
2. Different capital letters or extra spaces. Values typed by people or read from files often have a capital letter, a space or a newline you can't see.
colors = ["red", "green", "blue\n"] print("blue" in colors) # False: the list has 'blue\n'
3. A different type. Numbers read from input() or a file are text. The text "2" is never equal to the number 2.
ids = [1, 2, 3] ids.remove("2") # ValueError: x not in list
4. Removing items while looping over the same list. Each .remove() moves the later items one place to the left, so the loop skips the next item. Code that removes in a loop and then removes again later can end up asking for an item that is already gone.
nums = [1, 2, 2, 3] for n in nums: if n == 2: nums.remove(n) print(nums) # [1, 2, 3]: one 2 was skipped
How to fix it
Check with in before removing:
colors = ["red", "green"] if "blue" in colors: colors.remove("blue") print(colors) # ['red', 'green']
Or catch the error when a missing value is normal:
colors = ["red", "green"] try: position = colors.index("blue") except ValueError: position = -1 print(position) # -1
Make values match before comparing. Remove spaces and use one case, and convert numbers:
colors = [c.strip().lower() for c in ["Red", " green", "blue\n"]] colors.remove("blue") print(colors) # ['red', 'green'] ids = [1, 2, 3] ids.remove(int("2")) print(ids) # [1, 3]
Build a new list instead of removing in a loop. A list comprehension keeps the items you want and removes every copy of the others:
nums = [1, 2, 2, 3] nums = [n for n in nums if n != 2] print(nums) # [1, 3]
When you're not sure what the list holds, print it with repr() so spaces, newlines and quotes show up: print(repr(colors)).
When you'd see this in real code
- A to-do app where the reader types a task to delete, and the typed text has different capitals than the saved task.
- Lines read from a file with
readlines(), which keep the\nat the end of each line, so"admin"is not in the list but"admin\n"is. - A list of IDs as numbers, searched with an ID from a URL or a form, which is text.
- An "undo" or "unfollow" button pressed twice, so the second
.remove()looks for an item that is already gone. - In a shared list changed by two parts of the program, one part removes an item the other part still expects to find.
Related errors
- KeyError: the same "not found" problem for a dict key.
- IndexError: list index out of range: you asked for a position, not a value, and the position is past the end.
- RuntimeError: dictionary changed size during iteration: changing a dict while looping over it, which Python stops with an error instead of skipping items.
- ValueError: invalid literal for int() with base 10: when the text you convert with
int()isn't a number.
See Also
- All Python errors: the full index, by type and by when it happens.
- Lists (tutorial)
- Lists cheat sheet
- List comprehensions
- Exceptions (tutorial)
Found this useful? Share it with a friend who’s learning Python.