Last updated: Apr 9, 2024
Reading timeยท6 min
for
loopTo add elements to a list in a loop:
range()
class to get a range
object you can iterate over.for
loop to iterate over the range
object.list.append()
method to add elements to the list.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:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range 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.
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]
range()
class.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.
my_list = ['bobby', 'hadz'] my_list.append('com') print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The method returns None as it mutates the original list.
for
loopIf you need to add multiple elements to a list in a for
loop, use the
list.extend()
method.
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.
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.
To add items to a list while iterating over it:
range()
class to create a range
object according to the list's
length.range
object.list.append()
method to add items to the list while iterating.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)
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:
Name | Description |
---|---|
start | An integer representing the start of the range (defaults to 0 ) |
stop | Go up to, but not including the provided integer |
step | Range 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.
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.
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.
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.
my_list = ['bobby'] my_list.extend(['hadz', '.', 'com']) print(my_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
Alternatively, you can use the list.copy()
method.
This is a two-step process:
for
loop to iterate over a copy of the list.list.append()
method to add an item to the list.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.
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.
However, we can iterate over a copy of the list and modify the contents of the original list.
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.
a_list = ['bobby'] iterable = ['hadz', '.', 'com'] a_list.extend(iterable) print(a_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
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.
range
object or any other iterable to the list.extend()
method.a_list = ['bobby'] a_list.extend(range(1, 5)) print(a_list) # ๐๏ธ ['bobby', 1, 2, 3, 4]
If you need to insert the elements of the iterable into the list at a specific index, use list slicing.
a_list = ['bobby', 'hadz'] iterable = ('.', 'com') a_list[1:1] = iterable print(a_list) # ๐๏ธ ['bobby', '.', 'com', 'hadz']
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).
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.
This is a two-step process:
for
loop to iterate over the iterable.list.append()
method to add each element of the iterable to the
list.a_list = ['bobby'] iterable = ('hadz', '.', 'com') for item in iterable: a_list.append(item) print(a_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
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.
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.
You can learn more about the related topics by checking out the following tutorials: