Modify a list while iterating over it in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Modify a list while iterating over it in Python #

To modify a list while iterating over it:

  1. Use a for loop to iterate over a copy of the list.
  2. On each iteration, check if a certain condition is met.
  3. If the condition is met, modify the list.
main.py
my_list = [5, 11, 25, 7, 30] for item in my_list.copy(): if item <= 10: my_list.remove(item) print(my_list) # 👉️ [11, 25, 30]

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

main.py
my_list = [5, 11, 25, 7, 30] # 👇️ [5, 11, 25, 7, 30] 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 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 = [5, 11, 25, 7, 30] for item in my_list.copy(): if item <= 10: my_list.remove(item) print(my_list) # 👉️ [11, 25, 30]

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

If you aren't removing or adding new items to the list when iterating over it, you don't have to create a copy.

main.py
my_list = [5, 11, 25, 7, 30] for index, item in enumerate(my_list): if item <= 10: my_list[index] = 10 print(my_list) # 👉️ [10, 11, 25, 10, 30]

We used the enumerate() function to get access to the index of the current iteration.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index, and the second is the item.

The example checks if the current list item is less than or equal to 10 and if the condition is met, the item gets set to 10.

We aren't removing or adding items to the list while iterating, so creating a copy isn't necessary.

Alternatively, you can use a list comprehension.

Modify a list while iterating over it using list comprehension #

To modify a list while iterating over it:

  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 = [5, 11, 25, 7, 30] my_list[:] = [item for item in my_list if item > 10] print(my_list) # 👉️ [11, 25, 30]

We used a list comprehension to modify 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.

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 = [5, 11, 25, 7, 30] print(my_list[:]) # 👉️ [5, 11, 25, 7, 30]
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.

If you don't want to change the contents of the original list, use a list comprehension to create a new list.

main.py
my_list = [5, 11, 25, 7, 30] new_list = [item for item in my_list if item > 10] print(new_list) # 👉️ [11, 25, 30] print(my_list) # 👉️ [5, 11, 25, 7, 30]

We iterate over the list, check if each item is greater than 10 and return the result.

The new list only consists of elements that meet the condition.

The original list remains unchanged.

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.