You cannot remove an item from a list if it does not appear in said list. The Python error
ValueError: list.remove(x): x not in list tells you the item you want to remove does not appear in the list.
In this guide, we’re going to discuss the cause of the
ValueError: list.remove(x): x not in list. We will also discuss how to solve this error.
Python ValueError: list.remove(x): x not in list
ValueError: list.remove(x): x not in list Python error tells us we are using the remove() method to remove an item that does not appear in a list. The value
x will appear in the error message irrespective of the item you are trying to remove. For instance, if you tried to remove
123 from a list, you would still see the same error.
Let’s look at an example of this error in action.
An Example Scenario
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
We’re building a program that lets a teacher keep track of who has submitted their homework. To do this, we keep a list of all the students in a class who have been given the assignment. If a student hands in their homework, their name is removed from the list.
Let’s start by defining a list of students. Then, we will prompt the user to input a name that should be removed from the list. In the full program, we would save this data to a file. However, to keep things simple, we will not introduce files.
students = ["Mark", "Lindsay", "Peter"] to_remove = input("Enter the name of a student who has handed in their homework: ") students.remove(to_remove) print("This student's homework has been recorded.")
Let’s try to run our program:
Enter the name of a student who has handed in their homework: Markk
Our program returns:
Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list.remove(x): x not in list
Our program does not work because
Markk does not appear on our list.
To solve this error, we should first check if the student who we want to remove from our list appears in the list:
if to_remove in students: students.remove(to_remove) print("This student's homework has been recorded.") else: print("This student does not appear in your list.")
Let’s run our code again:
Enter the name of a student who has handed in their homework: Markk This student does not appear in your list.
Our code now returns a message rather than a Python error.
ValueError: list.remove(x): x not in list occurs when you try to remove an item from a list that does not appear in the list. You must use the remove() method to remove an item that does not exist in order for this error message to appear.
To solve the error, you should first check that the item that you want to remove exists in the list.
If you want to learn more about coding in Python, check out our How to Learn Python guide. You will find top tips on learning Python. The guide also contains some learning resources you can use to build your understanding of the Python programming language.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.