Add elements to a List in a Loop in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Add elements to a List in a Loop in Python #

To add elements to a list in a loop:

  1. Use the range() class to get a range object you can iterate over.
  2. Use a for loop to iterate over the range object.
  3. Use the list.append() method to add elements to the list.
main.py
my_list = ['bobby', 'hadz', 'com'] for i in range(3): my_list.append('new') # 👇️ ['bobby', 'hadz', 'com', 'new', 'new', 'new'] print(my_list)

We used the range() class to get a range object.

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]
If you need to iterate as many times as there are items in the list, pass the list's length to the range() class.
main.py
my_list = ['bobby', 'hadz', 'com'] for i in range(len(my_list)): my_list.append('new') # 👇️ ['bobby', 'hadz', 'com', 'new', 'new', 'new'] print(my_list)

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

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

The method returns None as it mutates the original list.

If you need to add multiple elements to a list in a for loop, use the list.extend() method.

main.py
my_list = ['bobby', 'hadz', 'com'] for i in range(2): my_list.extend(['1', '2']) # 👇️ ['bobby', 'hadz', 'com', '1', '2', '1', '2'] 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']

The list.extend method returns None as it mutates the original list.

Make sure you aren't iterating over the list and adding items to it as that would cause an infinite loop.

If you need to add elements to a list while looping over it, use the list.copy() method and create a copy of the list.

main.py
my_list = ['bobby', 'hadz', 'com'] for item in my_list.copy(): my_list.append('new') print(my_list) # 👉️ ['bobby', 'hadz', 'com', 'new', 'new', 'new']

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.