ValueError: list.remove(x) Not in List

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

Quick Answer

You are removing an element that does not exist in the list. Check with 'in' before calling remove(), or use try/except.

Why This Happens

list.remove() searches for the first occurrence and removes it. If not found, it raises ValueError. Use set.discard() if order does not matter.

The Problem

fruits = ['apple', 'banana']
fruits.remove('mango')

The Fix

fruits = ['apple', 'banana']
if 'mango' in fruits:
    fruits.remove('mango')

Step-by-Step Fix

  1. 1

    Check before removing

    Use 'if item in list:' before remove().

  2. 2

    Use try/except

    Wrap in try/except ValueError.

  3. 3

    Use set.discard()

    Sets have .discard() which silently handles missing items.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free