Skip to main content

How to Add Elements to Lists within Loops in Python

Dynamically adding elements to lists within loops is a fundamental technique in Python.

This guide explores various methods for adding elements to lists during iteration, including using append(), extend(), and discussing the crucial considerations of modifying a list while iterating over it.

Adding Single Elements with append() in a Loop

The most common way to add elements to a list within a loop is using the append() method.

Basic Appending with a for Loop

my_list = ['tutorial', 'reference', 'com']

for i in range(3):
my_list.append('new')

print(my_list) # Output: ['tutorial', 'reference', 'com', 'new', 'new', 'new']
  • my_list.append('new'): Adds the string 'new' to the end of my_list on each iteration.
  • The loop iterates three times using range(3), so "new" gets appended three times.

Using range for Simple Repetition

The range() function is frequently used with loops when you need to repeat an action a specific number of times:

my_list = ['tutorial', 'reference', 'com']
print(list(range(len(my_list)))) # Output: [0, 1, 2]
print(list(range(3))) # Output: [0, 1, 2]
print(list(range(5))) # Output: [0, 1, 2, 3, 4]
  • range(len(my_list) returns the list of indexes for the list my_list, which can be used to iterate over the list and modify it.

Adding Multiple Elements with extend() in a Loop

If you need to add multiple elements from another iterable (like a list) in each iteration of the loop, use the extend() method:

my_list = ['tutorial', 'reference', 'com']

for i in range(2):
my_list.extend(['1', '2'])

print(my_list) # Output: ['tutorial', 'reference', 'com', '1', '2', '1', '2']
  • The for loop will iterate twice.
  • my_list.extend(['1', '2']): Adds the elements '1' and '2' to the end of my_list during each iteration.

Modifying a List While Iterating Over It (with Caution)

Modifying a list while iterating over it directly can lead to unexpected behavior and is generally discouraged. You can either iterate over the range object to add elements to the list, or iterate over a copy of the list.

Iterating over a range using len()

my_list = ['tutorial', 'reference', 'com']

for index in range(len(my_list)):
my_list.append('another')
print(my_list) # Output: ['tutorial', 'reference', 'com', 'another', 'another', 'another']

  • The range(len(my_list)) will iterate over the original length of the list.
  • You can use the append or extend methods to add new items to the list.

Iterating Over a Copy

A safer alternative, particularly when removing items, is to iterate over a copy of the list:

my_list = ['tutorial', 'reference', 'com']

for item in my_list.copy(): # Iterate over a *copy*
my_list.append('another') # Modify the original list

print(my_list) # Output: ['tutorial', 'reference', 'com', 'another', 'another', 'another']
  • my_list.copy(): Creates a shallow copy of the list. Changes to my_list inside the loop won't affect the iteration.
  • In this example, my_list will grow with each iteration, but only for the number of times equal to the original length of my_list.

Adding all the elements from an iterable

If you need to add the elements from an iterable to the list, use the extend() method:

a_list = ['tutorial']
iterable = ['reference', '.', 'com']

a_list.extend(iterable) # Extends the list by adding all elements from iterable
print(a_list) # Output: ['tutorial', 'reference', '.', 'com']

a_list = ['tutorial']
iterable = ('.', 'com') # Using a tuple

a_list.extend(iterable)
print(a_list) # Output: ['tutorial', '.', 'com']

Inserting at a specific index

You can use the list slicing to insert a list into another list at a specific index:

a_list = ['tutorial', 'reference']
iterable = ('.', 'com')
a_list[1:1] = iterable
print(a_list) # Output: ['tutorial', '.', 'com', 'reference']
  • We are using the index 1 for both the start and the end of the slice, to effectively insert the elements from iterable.