Code with the error

basket = ["bread", "milk"]
basket.remove("cheese")
print(basket)

Error message (Python 3.12, last line)

ValueError: list.remove(x): x not in list

Corrected code

basket = ["bread", "milk"]
if "cheese" in basket:
    basket.remove("cheese")
print(basket)

Output

['bread', 'milk']

Common causes

  • list.remove() gets an item that is not in the list.
  • int() gets text that is not a whole number, such as "ten" or "3.5".
  • The number of names on the left differs from the number of values on the right.
  • list.index() searches for an item that is absent.

Practise in the course

Lesson: Lists

Frequently asked questions

How is ValueError different from TypeError?

TypeError is about the wrong kind of value, ValueError is about a wrong value of the right kind.

Why does int("ten") fail?

int() reads only digits with an optional sign and surrounding spaces. Number words are not converted.