BugHunt

ValueError: x is not in list

list.index() and list.remove() raise when the item is missing. Check with `in` first, or catch ValueError.

PythonNull and undefined errors

What it means

Unlike str.find(), which returns -1, list.index(x) raises ValueError when x is absent, and list.remove(x) does the same. Python 3.13 words it "list.index(x): x not in list"; older versions quote the value, as in "'zoe' is not in list".

Common causes

1. Looking up something that may be absent

index() assumes the item is there.

Breaks

position = queue.index(name)

Works

position = queue.index(name) if name in queue else -1

2. Removing an item that was already removed

remove() raises on the second call.

Breaks

tags.remove(tag)

Works

if tag in tags:
    tags.remove(tag)

3. A near miss: case, spaces or type

"Zoe" is not "zoe", and "3" is not 3.

Breaks

ids.index("3")   # ids holds ints

Works

ids.index(int("3"))

How to find it in your own code

Print the item and the list. If the item is genuinely absent, handle that case explicitly. If it looks present, compare repr() of both: a stray space, different case, or a string versus an int is the usual cause.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Other common errors