Remove items from a List while iterating in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove items from a List while iterating in Python #

Use a list comprehension to remove items from a list while iterating, e.g. new_list = [item for item in my_list if item > 100]. The list comprehension will return a new list containing only the elements that match the condition.

main.py
# ✅ Remove items from a List while iterating my_list = [77, 105, 123, 88, 199] new_list = [item for item in my_list if item > 100] print(new_list) # 👉️ [105, 123, 199] # --------------------------------------------- # ✅ Remove items from a list while iterating using a for loop for item in my_list.copy(): if item <= 100: my_list.remove(item) print(my_list) # 👉️ [105, 123, 199]

We used a list comprehension to remove the items from a list while iterating.

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 100 and return the result.

The new list will only contain values that are greater than 100.

The list comprehension in the example doesn't mutate the original list.

If you need to mutate the original list, assign to a slice that represents the entire list.

To remove items from a list while iterating:

  1. Use a list comprehension to iterate over the list.
  2. Check if each element meets a condition and return the result.
  3. Use list slicing to assign the result to the original list.
main.py
my_list = [77, 105, 123, 88, 199] my_list[:] = [item for item in my_list if item > 100] print(my_list) # 👉️ [105, 123, 199]

We used the my_list[:] syntax to get a slice that represents the entire list, so we can assign to the variable directly.

main.py
my_list = [77, 105, 123, 88, 199] print(my_list[:]) # 👉️ [77, 105, 123, 88, 199]

The slice my_list[:] represents the entire list, so when we use it on the left-hand side, we are assigning to the entire list.

This approach changes the contents of the original list.

Alternatively, you can use a for loop.

To remove items from a list while iterating:

  1. Use a for loop to iterate over a copy of the list.
  2. Check if each item meets a condition.
  3. Use the list.remove() method to remove the items that meet the condition.
main.py
my_list = [77, 105, 123, 88, 199] for item in my_list.copy(): if item <= 100: my_list.remove(item) print(my_list) # 👉️ [105, 123, 199]

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

main.py
my_list = [77, 105, 123, 88, 199] print(my_list.copy()) # 👉️ [77, 105, 123, 88, 199]

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.

main.py
my_list = [77, 105, 123, 88, 199] for item in my_list.copy(): if item <= 100: my_list.remove(item) print(my_list) # 👉️ [105, 123, 199]

On each iteration in the for loop, we check if the current item is less than or equal to 100 and remove the elements that meet the condition.

Here is another example that uses the same approach.

main.py
my_list = [77, None, 105, None, 123, None, 88, 199] for item in my_list.copy(): if item is None: my_list.remove(item) print(my_list) # 👉️ [77, 105, 123, 88, 199]

We used a for loop to remove all None values from a list while iterating over it.

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 iterate over the original list and modify its contents, 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.