The ValueError: list.remove(x): x not in list
occurs when you try to remove an element that is not present in the list using the list remove()
method. In this tutorial, we’ll understand this error and take a look at the steps that we can take to fix it with the help of some examples.
Understanding the ValueError: list.remove(x) is not in list
error
In Python, a list is a collection of items that are ordered and changeable. Lists in Python come with a number of useful built-in methods that help you work with and manipulate the contents of the list. One such method is the list remove()
method which lets you remove an item from a list. Pass the value that you want to remove from the list as an argument. If there are multiple occurrences of a value in a list, it removes the first occurrence of that value.
When using the list remove()
function, it is expected that the value you’re trying to remove is present in the list. If you try to remove a value that is not present in a list, it will raise the ValueError: list.remove(x) is not in list
error.
Let’s look at an example.
# create a list my_list = [1, 2, 3] # remove a value not present in the list my_list.remove(4)
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[3], line 4 2 my_list = [1, 2, 3] 3 # remove a value not present in the list ----> 4 my_list.remove(4) ValueError: list.remove(x): x not in list
In the above example, we created a list with three elements, 1, 2, and 3. We then tried to remove the element 4 using the list remove()
method. Since the element is not present in the list, we get the ValueError: list.remove(x): x not in list
error.
How to fix this error?
To fix the ValueError: list.remove(x): x not in list
error, make sure that the value that you’re trying to remove is actually present in the list. You can use the membership operator in
to check whether a value is present in at list or not. If the value is present, only then should to try to remove it to avoid the above ValueError
.
Let’s look at an example.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# create a list my_list = [1, 2, 3] # value to remove val = 4 # remove only if the value is present if val in my_list: # remove my_list.remove(val) else: print("Value not present in the list")
Output:
Value not present in the list
Here, we’re first checking the value val
is present in the list or not. Since the value is not present, we do not run the code with the remove()
method and instead print a message saying that the value is not present in the list.
Alternatively, you can also error handling using try except
to handle this error.
# create a list my_list = [1, 2, 3] # value to remove val = 4 try: my_list.remove(val) except ValueError: print("Value not present in the list")
Output:
Value not present in the list
Here, we have put the code with the remove()
function inside a try
block, if a ValueError
occurs, we handle it by printing the message that the value is not present in the list.
Good to know
A similar occurs if you try to use the list index()
function to find the index of an element that is not present in the list.
Let’s look at an example.
# create a list my_list = [1, 2, 3] # index of a value not present in the list idx = my_list.index(4)
Output:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[6], line 4 2 my_list = [1, 2, 3] 3 # index of a value not present in the list ----> 4 idx = my_list.index(4) ValueError: 4 is not in list
Note that the error message is not exactly the same but it is very similar.
Conclusion
The ValueError: list.remove(x) is not in list
error occurs when you try to remove an element that is not present in a list using the list remove()
method. To fix this error, you can use the in
keyword to check if the element is present in the list and then use the remove()
method or use a try-except block to catch the ValueError exception.
You might also be interested in –