fix valueerror list.remove(x) x not in list

How to Fix – ValueError: list.remove(x): x not in list

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.

fix valueerror list.remove(x) x not in list

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top