In this post, we discuss the ValueError: list.remove(#): # not in list in python. We will introduce you to how to apply the if statement or try/catch block to get rid of this problem, as well as the reason why we get this kind of error.
Why do we get the error message “ValueError: list.remove(#): # not in list” in Python
We use the remove() method in Python to remove the first matching element from the list. The problem here is when implementing this method and you receive the error “ValueError: list.remove(#): # not in list“. As you can see in the error message, Python tells you that the element you want to remove does not exist in the list – That is also the reason why you get this problem.
Here are two specific situations in how the error occurs:
Case 1:
flowers= ['rose', 'jasmine', 'tulip', 'orchid', 'iris'] flowers.remove('snowdrop')
Case 2:
flowers= ['rose', 'jasmine', 'tulip', 'orchid', 'iris'] flowers.remove('Orchid')
Error:
ValueError: list.remove(x): x not in list
In the first case, ‘snowdrop‘ is an element that does not exist in the list. When we ask Python to remove this element, an error will occur. On the other hand, in the second case, the remove() method is case sensitive, so ‘Orchid‘ is considered to not exist in the list even though the list contains an element named ‘orchid‘.
Solution for this problem
Before removing an element from the list, you need to check the existence of that element in the list. Doing so will keep your program from crashing.
Use the if statement
First, you can check an element’s existence by using the if statement.
flowers= ['rose', 'jasmine', 'tulip', 'orchid', 'iris'] target = 'snowdrop' # Only remove the element if it is in the list if target in flowers: flowers.remove(target ) print('Updated:' + flowers) else: print(target + ' is not in the list')
Output:
snowdrop is not in the list
The code snippet above checks if the element itself exists in the list. If it exists, the remove() method will be called. Otherwise, a message will be returned.
Use the try/catch block
Another approach is to use a try/catch block.
flowers= ['rose', 'jasmine', 'tulip', 'orchid', 'iris'] target = 'Orchid' # Remove the element; If there is an error, throw an exception try: flowers.remove(target) except ValueError: print(target + ' is not in list') print(flowers)
Output:
Orchidis not in list
['rose', 'jasmine', 'tulip', 'orchid', 'iris']
In this example, the try block will help us call the remove() method. If an error occurs, the catch block will catch the error and return a message.
Summary
In summary, you have just seen how to fix the ValueError: list.remove(#): # not in list in python. You have to ensure that you are removing an existing element from the list. To do that, we have shown you two approaches. You can choose to use the if statement or try/catch block. That’s the end of this article. Thanks for reading.
Maybe you are interested:
- ValueError: I/O operation on closed file in Python
- ValueError: object too deep for desired array
- ValueError: min() arg is an empty sequence in Python

My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js