Remove a value from a List if it exists in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove a value from a List if it exists in Python #

To remove a value from a list if it exists:

  1. Use the in operator to check if the value exists in the list.
  2. If the condition is met, use the list.remove() method to remove the value from the list.
main.py
my_list = ['one', 'two', 'three', 'four', 'five'] value_to_remove = 'two' # ✅ Remove value from list if it exists if value_to_remove in my_list: my_list.remove(value_to_remove) print(my_list) # 👉️ ['one', 'three', 'four', 'five'] # -------------------------------------------------- # ✅ Remove all occurrences of value from list if it exists my_list = ['one', 'two', 'three', 'two', 'two'] value_to_remove = 'two' while value_to_remove in my_list: my_list.remove(value_to_remove) print(my_list) # 👉️ ['one', 'three']

The first example uses the in operator to check if a value exists in a list before removing it.

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.

x not in l returns the negation of x in l.

If the value is present in the list, we use the list.remove() method to remove it.

main.py
my_list = ['one', 'two', 'three', 'four', 'five'] value_to_remove = 'two' if value_to_remove in my_list: my_list.remove(value_to_remove) print(my_list) # 👉️ ['one', 'three', 'four', 'five']

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

The remove() method mutates the original list and returns None.

This approach removes only the first occurrence of the specified value from the list.

If you need to remove all occurrences of the value, use a while loop.

To remove a value from a list if it exists:

  1. Use a while loop to iterate as long as the value is present in the list.
  2. Use the list.remove() method to remove the value from the list.
main.py
my_list = ['one', 'two', 'three', 'two', 'two'] value_to_remove = 'two' while value_to_remove in my_list: my_list.remove(value_to_remove) print(my_list) # 👉️ ['one', 'three']

The while loop iterates as long as the value to be removed exists in the list.

On each iteration, we use the list.remove() method to remove the value from the list.

This approach removes all occurrences of the specified value from the list.

Alternatively, you can use a for loop.

main.py
my_list = ['one', 'two', 'three', 'two', 'two'] value_to_remove = 'two' for item in my_list.copy(): if item == value_to_remove: my_list.remove(item) print(my_list) # 👉️ ['one', 'three']

The list.copy method returns a shallow copy of the object on which the method was called.

This is necessary because we aren't allowed to remove items from a list while iterating over it.

However, we can iterate over a copy of the list and remove items from the original list.

main.py
my_list = ['one', 'two', 'three', 'two', 'two'] value_to_remove = 'two' for item in my_list.copy(): if item == value_to_remove: my_list.remove(item) print(my_list) # 👉️ ['one', 'three']
On each iteration, we check if the current item is equal to the value to be removed and use the list.remove() method to remove the matching elements.

The most important thing to note when removing items from a list in a for loop is to use the list.copy() method to iterate over a copy of the list.

If you try to iterate over the original list and remove items from it, you might run into difficult to locate bugs.

An alternative to checking if the value exists in the list before removing it is to use a try/except statement.

main.py
my_list = ['one', 'two', 'three', 'four', 'five'] value_to_remove = 'apple' try: my_list.remove(value_to_remove) except ValueError: # 👇️ this runs pass print(my_list) # 👉️ ['one', 'two', 'three', 'four', 'five']

The list.remove() method raises a ValueError if the provided value doesn't exist in the list.

If the value is not present in the list, the except block runs.

You can use a pass statement if you need to ignore the error.

The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.

You can also use a try/except statement if you need to remove all occurrences of the specified value from the list, but it's a bit more difficult to read than checking if the value exists in the list before removing it.

main.py
my_list = ['one', 'two', 'three', 'two', 'two'] value_to_remove = 'two' while True: try: my_list.remove(value_to_remove) except ValueError: break print(my_list) # 👉️ ['one', 'three']

The while loop iterates until the break statement runs.

The break statement breaks out of the innermost enclosing for or while loop.

On each iteration, we used the list.remove() method to remove the value from the list.

If the value is not present in the list, the except block runs where we exit out of the while loop.

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.