Remove elements from list using list comprehension in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove elements from list using list comprehension in Python #

To use a list comprehension to remove elements from a list:

  1. Iterate over the list using a list comprehension.
  2. Filter out all elements that meet a certain condition.
  3. The new list won't contain any of the specified elements.
main.py
my_list = [1, 5, 10, 15, 25, 30] # ✅ remove all elements from list that are greater than 10 new_list = [item for item in my_list if item <= 10] print(new_list) # 👉️ [1, 5, 10] # ----------------------------------------- # ✅ remove first element from list that is greater than 10 for item in my_list.copy(): if item > 10: my_list.remove(item) break print(my_list) # 👉️ [1, 5, 10, 25, 30] # ----------------------------------------- # ✅ remove item from list my_list.remove(30) print(my_list) # 👉️ [1, 5, 10, 25]

The first example uses a list comprehension to remove elements from a list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
main.py
my_list = [1, 5, 10, 15, 25, 30] new_list = [item for item in my_list if item <= 10] print(new_list) # 👉️ [1, 5, 10]

On each iteration, we check if the current item is less than or equal to 10 and return the result.

The new list only contains items that are less than or equal to 10.

Using a list comprehension is suitable if you need to remove one or more elements from a list.

The list comprehension keeps iterating after the first matching element has been removed.

Note that the list comprehension doesn't mutate the original list. It returns a new list.

If you need to remove only the first matching element from a list, use a for loop.

main.py
my_list = [1, 5, 10, 15, 25, 30] for item in my_list.copy(): if item > 10: my_list.remove(item) break print(my_list) # 👉️ [1, 5, 10, 25, 30]

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 modify a list's contents while iterating over it.

However, we can iterate over a copy of the list and modify the contents of the original list.

On each iteration, we check if the current item is greater than 10 and use the list.remove() method to remove the matching item.

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

We used the break statement to stop iterating after the matching element has been removed.

This is not possible in a list comprehension.

If you have the value of the list item you need to remove, use the list.remove() method.

main.py
my_list = [1, 5, 10, 15, 25, 30] my_list.remove(30) print(my_list) # 👉️ [1, 5, 10, 15, 25]

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.

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

If you need to handle a scenario where the item is not present in the list, use a try/except statement.

main.py
my_list = ['a', 'b', 'c'] try: my_list.remove('z') except ValueError: print('Item not in list') print(my_list) # 👉️ ['a', 'b', 'c']
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.