Remove list elements less than a given value in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove list elements less than a given value in Python #

Use a list comprehension to remove the list elements that are less than a given value, e.g. new_list = [item for item in my_list if item >= 100]. The list comprehension will return a new list without the elements that are less than the specified value.

main.py
my_list = [47, 88, 99, 120, 234, 567] # ✅ Remove list elements less than a given value (list comprehension) new_list = [item for item in my_list if item >= 100] print(new_list) # 👉️ [120, 234, 567] # ---------------------------------------------- # ✅ Remove list elements less than a given value (for loop) for item in my_list.copy(): if item < 100: my_list.remove(item) print(my_list) # 👉️ [120, 234, 567]

We used a list comprehension to remove the items from a list that are less than a given value.

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 greater than or equal to the specified value and return the result.

main.py
my_list = [47, 88, 99, 120, 234, 567] new_list = [item for item in my_list if item >= 100] print(new_list) # 👉️ [120, 234, 567]
The new list consists of the elements that are not less than the specified value.

The list comprehension doesn't mutate the original list, it returns a new list.

Alternatively, you can use a for loop.

To remove the elements from a list that are less than a given value:

  1. Use a for loop to iterate over a copy of the list.
  2. Check if each item is less than the given value.
  3. Use the list.remove() method to remove the items that meet the condition.
main.py
my_list = [47, 88, 99, 120, 234, 567] for item in my_list.copy(): if item < 100: my_list.remove(item) print(my_list) # 👉️ [120, 234, 567]

We used the list.copy() method to get a copy of the list.

main.py
my_list = [47, 88, 99, 120, 234, 567] # 👇️ [47, 88, 99, 120, 234, 567] print(my_list.copy())

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 elements from a list while iterating over it.

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

main.py
my_list = [47, 88, 99, 120, 234, 567] for item in my_list.copy(): if item < 100: my_list.remove(item) print(my_list) # 👉️ [120, 234, 567]

On each iteration, we check if the current list item is less than 100 and if the condition is met, we use the list.remove() method to remove it.

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

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

If you try to remove elements from the original list while iterating over it, you might run into difficult to locate bugs.

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.