If you try to remove an element from a list that does not appear in that list, you will raise the ValueError: list.remove(x) x not in list. To solve this error, you can check for list membership using the in operator, for example, if x in a_list
.
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
ValueError: list.remove(x) x not in list
In Python, a value is information stored within a particular object. We will encounter a ValueError in Python when you use a built-in operation or function that receives an argument with the right type but an inappropriate value.
For this ValueError, we have a suitable item type, but an item that does not exist in the list is not a suitable value.
Let’s look at the syntax for the list.remove()
list.remove(element)
Parameters
element
: Required. The element to remove. Can be any type.
This method raises a ValueError if there is no such element in the list.
Example #1: Removing an Element that does not Exist in List
Let’s look at an example where we attempt to remove a number from a list of numbers. We will use the input()
function to get a number from the user. Let’s look at the code:
numbers = [1, 2, 3, 4, 5, 6, 7, 8] number_to_remove = int(input("Enter a number to remove from the list: ")) numbers.remove(number_to_remove) print(numbers)
In the above code, we assign an integer value to the variable number_to_remove
, then call the remove()
method on the numbers list to remove that number. Let’s run the code to see the result:
Enter a number to remove from the list: 10 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-6-41c0a9b2e69c> in <module> 3 number_to_remove = int(input("Enter a number to remove from the list: ")) 4 ----> 5 numbers.remove(number_to_remove) 6 7 print(numbers) ValueError: list.remove(x): x not in list
The error occurs because the number 10 does not exist in the list.
Solution
To solve this error, we can check if the number exists in the list before removing it using an if...in
statement. The in
operator checks for membership in the list. Let’s look at the revised code:
numbers = [1, 2, 3, 4, 5, 6, 7, 8] number_to_remove = int(input("Enter a number to remove from the list: ")) if number_to_remove in numbers: numbers.remove(number_to_remove) print('Number removed') else: print(f'number to remove {number_to_remove} not found in list') print(numbers)
In the above code, we call the remove()
method on the list if the number exists in the list; otherwise, we print that the number was not found
. Let’s run the code and input a number that does not exist in the list.
Enter a number to remove from the list: 10 number to remove 10 not found in list [1, 2, 3, 4, 5, 6, 7, 8]
Next, let’s run the code and input a number that does exist in the list:
Enter a number to remove from the list: 5 Number removed [1, 2, 3, 4, 6, 7, 8]
Example #2: Removing Multiple Items from a List
Let’s look at an example where we want to remove multiple strings from a list of strings. We will attempt to remove two strings by calling the remove()
method and passing it a single string containing the names of two vegetables.
vegetables = ["spinach", "asparagus", "celery", "carrot", "kale"] vegetables.remove("spinach, asparagus") print(vegetables)
Let’s run the code to see the result:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-18-28dd879f38c5> in <module> 1 vegetables = ["spinach", "asparagus", "celery", "carrot", "kale"] ----> 2 vegetables.remove("spinach, asparagus") 3 print(vegetables) ValueError: list.remove(x): x not in list
The error occurs because the item "spinach, asparagus"
does not exist in the list, and only the individual strings "spinach"
and "asparagus"
exist.
Solution #1: Remove Each Element One at a Time
To solve this error, we have to remove each element separately. We cannot remove both using a single string because that has a different value. Let’s look at the revised code:
vegetables = ["spinach", "asparagus", "celery", "carrot", "kale"] vegetables.remove("spinach") vegetables.remove("asparagus") print(vegetables)
Let’s run the code to see the result:
['celery', 'carrot', 'kale']
Solution #2: Use a For Loop
We can also use a for loop so that we only need to write the remove()
line of code once. We store the elements to remove in a separate list, veg_to_remove
. Then, we loop over that list and call the remove()
method on vegetables to remove those elements. Let’s look at the revised code:
vegetables = ["spinach", "asparagus", "celery", "carrot", "kale"] veg_to_remove = ["spinach", "asparagus"] for item in veg_to_remove: vegetables.remove(item) print(vegetables)
Let’s run the code to see the result:
['celery', 'carrot', 'kale']
Summary
Congratulations on reading to the end of this tutorial! The ValueError: list.remove(x): x not in list occurs when you try to remove an element that does not exist in a list. You can solve this error by checking if the element exists in the list before trying to remove it using the in operator. If you want to remove multiple elements from a list, you have to call the remove()
method for each element.
Go to the Python online courses page to learn more about coding in Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.