Last updated: Apr 10, 2024
Reading timeยท4 min
Use the list.extend()
method to append multiple values to a list.
The list.extend()
method will append the items of the provided iterable to
the list.
a_list = ['bobby'] a_list.extend(['hadz', '.', 'com']) print(a_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
If you need to insert multiple elements into a list at a specific index, click on the following subheading:
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.
extend()
method, but you can pass it any other iterable, e.g. a tuple or a range
object.a_list = ['bobby'] a_tuple = ('hadz', '.', 'com') a_list.extend(a_tuple) print(a_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
Here is an example that uses a tuple.
a_list = ['bobby'] a_tuple = ('hadz', '.', 'com') a_list.extend(a_tuple) print(a_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
This is a two-step process:
for
loop to iterate over the collection of values.list.append()
method to append each value to the list.list1 = ['bobby'] list2 = ['hadz', '.', 'com'] for item in list2: list1.append(item) print(list1) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
We used a for loop to iterate over the collection of values.
On each iteration, we use the list.append()
method to append the current value
to the list.
Alternatively, you can use the addition (+) operator.
When the addition (+) operator is used with two lists, it combines the lists into a single list.
a_list = ['bobby'] multiple_values = ['hadz', '.', 'com'] a_list = a_list + multiple_values print(a_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
When the addition (+) operator is used with lists, it combines the lists into a single list.
print([1] + [2, 3, 4]) # ๐๏ธ [1, 2, 3, 4]
list.extend()
method directly.Alternatively, you can use the itertools.chain()
method.
The itertools.chain()
class can be used to treat multiple lists as a single
list.
from itertools import chain a_list = ['bobby'] multiple_values = ['hadz', '.', 'com'] a_list = list(chain(a_list, multiple_values)) print(a_list) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
The itertools.chain() class is used to treat multiple lists as a single list.
import itertools first = [1, 2] second = [3, 4] third = [5, 6] for item in itertools.chain(first, second, third): print(item) # ๐๏ธ 1 2 3 4 5 6
The itertools.chain()
class creates an iterator from the provided iterables.
The class is used for treating multiple sequences as a single sequence.
Which approach you pick is a matter of personal preference. I'd use the
list.extend()
method as it is quite performant and easy to read.
Another simple approach to append multiple values to a list is to use a for
loop.
Use list slicing assignment to
insert multiple elements into a list at a specific index, e.g.
list1[1:1] = list2
.
The elements of the second list will get inserted into the first at the given index.
list1 = ['bobby', 'hadz'] list2 = ['.', 'com'] list1[1:1] = list2 print(list2) # ๐๏ธ ['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
.list1 = ['bobby', 'hadz'] list2 = ['.', 'com'] list1[0:0] = list2 print(list2) # ๐๏ธ ['bobby', '.', 'com', 'hadz']
The assignment adds the items of the second list to the first list at the specified index.
If you need to insert multiple elements at the end of a list, use the
list.extend()
method.
list1 = ['bobby', 'hadz'] list2 = ['.', 'com'] list1.extend(list2) print(list1) # ๐๏ธ ['bobby', 'hadz', '.', 'com']
The list.extend() method takes an iterable 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.
This is a three-step process:
list1 = ['bobby', 'hadz'] list2 = ['.', 'com'] index = 1 list1 = list1[:index] + list2 + list1[index:] print(list1) # ๐๏ธ ['bobby', '.', 'com', 'hadz']
When used with lists, the addition (+) operator combines the lists.
print(['a'] + ['b'] + ['c']) # ๐๏ธ ['a', 'b', 'c']
The slice list1[:index]
starts at index 0
and goes up to, but not including
the specified index.
The slice list1[index:]
starts at the specified index and goes to the end of
the list.
The last step is to use the addition (+) operator to combine the elements of the lists.
You can learn more about the related topics by checking out the following tutorials: