How to Add elements to a List in a Loop in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
6 min

banner

# Table of Contents

  1. Add elements to a List in a Loop in Python
  2. Add multiple elements to a list in a for loop
  3. Add items to a List while iterating over it in Python
  4. Add all elements of an iterable to a List in Python

# 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)

add elements to list in loop

The code for this article is available on GitHub

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)

iterating as many times as there are items in the list

The code for this article is available on GitHub

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.

# Add multiple elements to a list in a for loop

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)

add multiple elements to list in for loop

The code for this article is available on GitHub

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.

# 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)

add items to list while iterating over it

The code for this article is available on GitHub
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()

This is a two-step process:

  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)

add items to list while iterating over it using list copy

The code for this article is available on GitHub

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.

# Add all elements of an iterable to a List in Python

Use the list.extend() method to add all elements of an iterable to a list.

The list.extend() method will append the elements of the iterable to the list.

main.py
a_list = ['bobby'] iterable = ['hadz', '.', 'com'] a_list.extend(iterable) print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']
The code for this article is available on GitHub

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

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

You can pass a list, a tuple, a range object or any other iterable to the list.extend() method.
main.py
a_list = ['bobby'] a_list.extend(range(1, 5)) print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 1, 2, 3, 4]

# Insert the elements of the iterable into the list at a specific index

If you need to insert the elements of the iterable into the list at a specific index, use list slicing.

main.py
a_list = ['bobby', 'hadz'] iterable = ('.', 'com') a_list[1:1] = iterable print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', '.', 'com', 'hadz']
The code for this article is available on GitHub

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

The list slicing assignment inserts the elements of the iterable into the list at the specified index.

Alternatively, you can use a for loop.

# Add all elements of an iterable to a List using a for loop

This is a two-step process:

  1. Use a for loop to iterate over the iterable.
  2. Use the list.append() method to add each element of the iterable to the list.
main.py
a_list = ['bobby'] iterable = ('hadz', '.', 'com') for item in iterable: a_list.append(item) print(a_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', '.', 'com']
The code for this article is available on GitHub

We used a for loop to iterate over the iterable.

On each iteration, we use the list.append() method to append the current element to the 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.

Note that using the list.extend() method is more performant than calling list.append() for each element in the iterable.

I've also written an article on how to add items to a dictionary in a loop.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev