ValueError: list.remove(x): x not in list in Python [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
6 min

banner

# Table of Contents

  1. ValueError: list.remove(x): x not in list
  2. ValueError: 'X' is not in list when using index()

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

The Python "ValueError: list.remove(x): x not in list" occurs when we call the remove() method with a value that does not exist in the list.

To solve the error, check if the value exists in the list before removing it, or use a try/except block.

valueerror list remove x not in list

Here is an example of how the error occurs.

main.py
my_list = ['bobby', 'hadz', 'com'] # โ›”๏ธ ValueError: list.remove(x): x not in list my_list.remove('another')

value is not in the list

We passed a value that is not in the list to the remove() method which caused the error.

# Check if the element is present in the list before calling remove()

One way to solve the error is to check if the value is present in the list before passing it to the remove() method.

main.py
my_list = ['bobby', 'hadz', 'com'] if 'another' in my_list: my_list.remove('another') print(my_list) else: # ๐Ÿ‘‡๏ธ This runs print('The value is not in the list')

check if element in list before calling remove

The in operator tests for membership.

For example, x in l evaluates to True if x is a member of l, otherwise, it evaluates to False.

main.py
my_list = ['bobby', 'hadz', 'com'] print('bobby' in my_list) # ๐Ÿ‘‰๏ธ True print('another' in my_list) # ๐Ÿ‘‰๏ธ False

# Using a try/except statement to handle the error

You can also use a try/except statement to handle the error in case the value is not present in the list.

main.py
my_list = ['bobby', 'hadz', 'com'] try: my_list.remove('another') except ValueError: # ๐Ÿ‘‡๏ธ This runs print('The value is NOT in list') print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

using try except statement to handle the error

We call the remove() method on the list and if a ValueError is raised, the except block is run.

The list.remove() method removes the first item from the list whose value is equal to the passed-in argument.

main.py
my_list = ['a', 'b', 'c'] my_list.remove('a') print(my_list) # ๐Ÿ‘‰๏ธ ['b', 'c']

The method raises a ValueError if there is no such item.

# Removing multiple items from a list

The list.remove() method can only be called with a single argument.

You can use a for loop to remove multiple items from a list.

main.py
my_list = ['bobby', 'hadz', 'com'] items_to_remove = ['bobby', 'com'] for item in items_to_remove: if item in my_list: my_list.remove(item) print(my_list) # ๐Ÿ‘‰๏ธ ['hadz']

remove multiple items from list

We stored the values we wanted to remove in a list and used a for loop to iterate over the list.

On each iteration, we check if the current item is contained in the list before calling the list.remove() method.

You can also use a try/except statement to handle a potential error while iterating.

main.py
my_list = ['bobby', 'hadz', 'com'] items_to_remove = ['bobby', 'com', 'ab', 'cd'] for item in items_to_remove: try: my_list.remove(item) except ValueError: print('The item is not in the list') print(my_list) # ๐Ÿ‘‰๏ธ ['hadz']

If any of the items in the list are not contained in the original list, the except block runs where the ValueError is handled.

# Using a list comprehension instead of list.remove()

As an alternative to using the list.remove() method, you can also use a list comprehension.

main.py
my_list = ['bobby', 'hadz', 'com'] items_to_remove = ['bobby', 'com'] my_list = [item for item in my_list if item not in items_to_remove] print(my_list) # ๐Ÿ‘‰๏ธ ['hadz']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current item is not contained in the list of items to be removed.

If the condition is met, the item gets included in the new list.

# Removing items from a two-dimensional list

If you have a two-dimensional list, make sure you are calling the remove() method on the correct list.

main.py
my_list = [['a', 'b'], ['c', 'd']] my_list[0].remove('b') print(my_list) # ๐Ÿ‘‰๏ธ [['a'], ['c', 'd']]

We accessed the list item at index 0 and called the remove() method on it.

Had we called the remove() method on the outer list, we would get a ValueError because it doesn't contain the string "b".

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(a_list) - 1.

main.py
my_list = [['a', 'b'], ['c', 'd']] print(my_list[0]) # ๐Ÿ‘‰๏ธ ['a', 'b'] print(my_list[1]) # ๐Ÿ‘‰๏ธ ['c', 'd']

# Make sure the value you are trying to remove is the correct type

The following code sample also causes the error.

main.py
my_list = [1, 2, 3, 4] value = '3' # โ›”๏ธ ValueError: list.remove(x): x not in list my_list.remove(value)

We have a list of integers, but the value we are trying to remove is a string.

To solve the error, convert the string to an integer before calling remove().

main.py
my_list = [1, 2, 3, 4] value = '3' my_list.remove(int(value)) print(my_list) # ๐Ÿ‘‰๏ธ [1, 2, 4]

We used the int() class to convert the string to an integer in the call to list.remove().

The types of values have to match, otherwise, the error is raised.

# ValueError: 'X' is not in list when using index()

The Python "ValueError: is not in list" occurs when we use the index() method with a value that is not present in the list.

To solve the error, check if the value is in the list before using the index method, e.g. if 'value' in my_list:, or use a try/except block.

valueerror is not in list

Here is an example of how the error occurs.

main.py
my_list = ['apple', 'banana', 'kiwi'] # โ›”๏ธ ValueError: 'melon' is not in list idx = my_list.index('melon')

We passed a value that is not in the list to the index() method which caused the error.

# Checking if the value is in the list before calling index()

One way to solve the error is to check if the value is present in the list before calling the index() method.

main.py
my_list = ['apple', 'banana', 'kiwi'] if 'melon' in my_list: idx = my_list.index('melon') print(idx) else: # ๐Ÿ‘‡๏ธ this runs print('value is not in my list')

The in operator tests for membership.

main.py
my_list = ['apple', 'banana', 'kiwi'] print('apple' in my_list) # ๐Ÿ‘‰๏ธ True print('another' in my_list) # ๐Ÿ‘‰๏ธ False

For example, x in l evaluates to True if x is a member of l, otherwise, it evaluates to False.

The list.index() method returns the index of the first item whose value is equal to the provided argument.

main.py
my_list = ['apple', 'banana', 'kiwi'] result = my_list.index('banana') print(result) # ๐Ÿ‘‰๏ธ 1

The method raises a ValueError if there is no such item in the list.

# Using a try/except statement to handle the error

You can also use a try/except statement to handle the scenario where the item is not in the list.

main.py
my_list = ['apple', 'banana', 'kiwi'] try: idx = my_list.index('melon') print(idx) except ValueError: print('item is not in the list') # ๐Ÿ‘‰๏ธ this runs

We call the index() method on the list and if a ValueError is raised, the except block is run.

# Using a one-liner if/else statement to handle the error

You can also use a one-liner if/else statement.

main.py
my_list = ['apple', 'banana', 'kiwi'] result_1 = my_list.index('melon') if 'melon' in my_list else None print(result_1) # ๐Ÿ‘‰๏ธ None result_2 = my_list.index('apple') if 'apple' in my_list else None print(result_2) # ๐Ÿ‘‰๏ธ 0
If the value is present in the list, we return the result of calling the index() method with the value, otherwise we return None.

# Calling the index() method on the correct list

If you have a two-dimensional list, make sure you are calling the index() method on the correct list.

main.py
my_list = [['apple', 'banana'], ['kiwi', 'mango']] idx = my_list[0].index('apple') print(idx) # ๐Ÿ‘‰๏ธ 0

We accessed the list item at index 0 and called the index() method on it.

Had we called the index() method on the outer list, we would get a ValueError because it doesn't contain the string "apple".

Want to learn more about removing items from a list in Python? Check out these resources: Remove elements from a List while iterating in Python,Remove elements from a List based on a condition in Python.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev