ValueError: list.remove(x): x not in list in Python

In Python, the “List” data structure is used to store the ordered collection of data that is mutable (changeable). In Python, several functions are used to manipulate the “List”, such as removing, adding, replacing, etc. One such function, “list.remove()”, is used to remove the “elements/items” from the list. The error arises when we try to remove the “elements” that do not exist in the list.

This Python write-up will provide a comprehensive guide on rectifying the “ValueError: list.remove(x): x not in list” in Python. This guide will cover the following concepts:

Reason: Calling “list.remove()” Method on Value Not Present in the List

The prominent reason which causes this error in Python is when the user tries to call the “list.remove()” method on the list that does not contain the specified value. For instance, the below snippet shows the stated error:

The above output shows the “ValueError” because the string value “Alex” is not present in the input list.

Solution 1: Check the Value Before Removing

To resolve this error, we need to check whether the value is present in the list. An example code is given below:

Code:

list_value = ['John', 50, 'Lily']
if 'Alex' in list_value:
    list_value.remove('Alex')
    print(list_value)
else:
    print('value is nor present in the list')

In the above code:

  • The “if-else” statement is utilized to check whether the specified element “Alex” is present in the list or not.
  • If the value is present in the list, then the “if” block will be executed; consequently, it will remove the element from the list.
  • The “else” block will be executed if the item is not in the input list.

Output:

The above output shows that the removed value is not present in the list.

Solution 2: Use try-except Block

The “try-except” block is also used to remove the “ValueError: list.remove(x): x not in list” in the Python program. For example, look at the code below:

Code:

list_value = ['John', 50, 'Lily']
try:
    list_value.remove('Alex')
    print(list_value)
except ValueError:
    print('value is nor present in the list')

In the above code:

  • The ” try” block executes its code only if the value to be removed is present in the list.
  • The “except” block executes its code when the error “list.remove(x): x not in list” arises in the program. In such a case, the user-specified message will be displayed instead of throwing the specified error.

Output:

The above output indicates that the value to be removed is not present in the list.

Conclusion

In Python, the “list.remove(x): x not in list” error occurs when a user tries to delete the element that does not exist in the list. To resolve this error, check the element value before removing it, or use the try-except block to handle the ValueError in Python. This Python guide presented various reasons and solutions for the value error “ x not in list”  using appropriate examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×