Modify the elements of a List in a For loop in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Modify the elements of a List in a For loop in Python #

To modify the elements of a list in a for loop:

  1. Use the enumerate() function to get access to the index of the current iteration.
  2. Use the list() class to convert the enumerate object to a list.
  3. Modify the elements of the list.
main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in list(enumerate(my_list)): my_list[index] = item + str(index) my_list.append('new') print(my_list) # 👉️ ['bobby0', 'hadz1', 'com2', 'new', 'new', 'new']

Notice that we used the list() class to convert the enumerate() object to a list.

This is necessary if you need to add items to the list or remove items from it in a for loop.

If you only need to update values in the list and won't be changing the list's length, you don't have to use the list() class.

main
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): # 👇️ only updating values in the list my_list[index] = item + str(index) print(my_list) # 👉️ ['bobby0', 'hadz1', 'com2']

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 corresponding item.

main.py
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 bobby, 1 hadz, 2 com
If you only need to update values in the list and won't be changing the list's length, you can iterate directly over the list or an enumerate object.

However, you can't add values to a list while iterating over it because this causes an infinite loop.

Similarly, you can't remove items from the list while iterating over it because the list's length changes every time an item is removed.

Alternatively, you can use the range() class.

Modify the elements of a List in a For loop using range() #

To modify the elements of a list in a for loop:

  1. Use the range() class to get a range object according to the list's length.
  2. Use a for loop to iterate over the range object.
  3. Modify the elements of the list.
main.py
my_list = ['bobby', 'hadz', 'com'] for index in range(len(my_list)): my_list[index] = my_list[index] + str(index) my_list.append('new') print(my_list) # 👉️ ['bobby0', 'hadz1', 'com2', 'new', 'new', 'new']

The range class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

main.py
my_list = ['bobby', 'hadz', 'com'] print(list(range(len(my_list)))) # 👉️ [0, 1, 2] print(list(range(4))) # 👉️ [0, 1, 2, 3] print(list(range(5))) # 👉️ [0, 1, 2, 3, 4]

We used the len() function to get a range object with the same length as the list.

When using this approach you can add, update or remove items from the list.

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.