Add items to a List while iterating over it in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Add items to a List while iterating over it in Python #

To add items to a list while iterating over it:

  1. Use the range() class to create a range object according to the list's length.
  2. Iterate over the range object.
  3. Use the list.append() method to add items to the list while iterating.
main.py
my_list = ['bobby', 'hadz', 'com'] for index in range(len(my_list)): my_list.append('another') # 👇️ ['bobby', 'hadz', 'com', 'another', 'another', 'another'] print(my_list)
Instead of iterating over the list, we iterate over a range object that was created using the list's length.

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(3))) # 👉️ [0, 1, 2]

The list.append() method adds an item to the end of the list.

main.py
my_list = ['bobby', 'hadz', 'com'] for index in range(len(my_list)): my_list.append('another') # 👇️ ['bobby', 'hadz', 'com', 'another', 'another', 'another'] print(my_list)

If you need to add multiple items to the list while iterating, use the list.extend() method.

main.py
my_list = ['bobby', 'hadz', 'com'] for index in range(len(my_list)): my_list.extend(['one', 'two']) # 👇️ ['bobby', 'hadz', 'com', 'one', 'two', 'one', 'two', 'one', 'two'] print(my_list)

The list.extend method takes an iterable (such as a list) and extends the list by appending all of the items from the iterable.

main.py
my_list = ['bobby'] my_list.extend(['hadz', '.', 'com']) print(my_list) # 👉️ ['bobby', 'hadz', '.', 'com']

Alternatively, you can use the list.copy() method.

Add items to a List while iterating over it using list.copy() #

To add items to a list while iterating over it:

  1. Use a for loop to iterate over a copy of the list.
  2. On each iteration, use the list.append() method to add an item to the list.
main.py
my_list = ['bobby', 'hadz', 'com'] for item in my_list.copy(): my_list.append('another') # 👇️ ['bobby', 'hadz', 'com', 'another', 'another', 'another'] print(my_list)

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

main.py
my_list = ['bobby', 'hadz', 'com'] print(my_list.copy()) # 👉️ ['bobby', 'hadz', 'com']

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.

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.