How to fix the ValueError: list.remove(#): # not in list in Python

ValueError: list.remove(#): # not in list in python

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:

Leave a Reply

Your email address will not be published. Required fields are marked *