The list
is one of the most used container data types in Python. It allows us to represent a sequence of elements in an organized and easy to access manner. One of the most useful feature of lists is their mutable nature, it makes it possible to perform operations that can modify the actual list without creating a new list.
In this article you will learn how to add multiple elements to a list.
To add multiple elements to a list we can use a various approaches:
- Using the
extend()
method. - Using the
append()
method with a loop. - Using
insert()
method with a loop
Using the extend() method
The extend()
method offers a convenient way to add elements from an iterable object into a list.
# a list of strings
mylist = ['one', 'two', 'three']
mytuple = ('four', 'five', 'six')
#add the tuple elements to the list
mylist.extend(mytuple)
print(mylist)
The syntax of the extend()
method is as shown below:
lst.extend(iterable)
iterable |
Required. An iterable object(list, tuple, set, range,etc) containing the elements to be added to the list. |
The extend()
methods adds the elements of the input iterable at the back of the list.
mylist = [1, 2, 3, 4]
mylist.extend(range(5, 10))
print(mylist)
In the above example, we extended the list using elements from a range object.
Using the append() method with a loop
The append()
method adds a single element at the back of a list. We can call it repeatedly using a loop in order to add multiple elements to the list.
#a list of strings
mylist = ['Japan', 'Rwanda', 'India']
#tupkle containing the additional elements
other = ('Singapore', 'Canada', 'China')
for item in other:
mylist.append(item)
print(mylist)
In the above example:
- We have a list of strings,
mylist
. - tuple,
mytuple
contains the additional elements that are to be added to the list. - The for
loop
iterate through the tuple elements - The
append()
method is called during each iteration in order to add the current element at the back of the list. - By the time the loop terminates, all the elements in the tuple have been appended to the list.
Using insert() method with a loop
The insert() method is used to add an element at an arbitrary position in the list. It takes an element and an index where it should be inserted in the list.
lst.insert(index, item)
We can call the method repeatedly with a loop so as to add multiple elements in the list.
add multiple elements at the front of the list
mylist = [5, 6, 7, 8, 9]
for item in range(5, 0, -1):
mylist.insert(0, item)
print(mylist)
In the above example we used the insert()
method to add multiple elements at the front of the list. By passing 0 as the index argument to the method, we ensures an element during each iteration is added at the front of the list.
add multiple elements at the back of the list
mylist = [1, 2, 3, 4]
for item in range(5, 10):
mylist.insert(len(mylist), item)
print(mylist)
In the above example, by passing the length of the list as the index argument to the insert()
method, we ensures that the new elements are added at the back of the list.