Introduction
Lists are probably the most commonly used data structures in Python as their characteristics are suitable for many different use-cases. Since this particular object type is mutable (i.e. it can be modified in place), adding or removing elements is even more common.
In today’s article, we are going to explore the difference between the two built-in list methods append()
and extend()
that can be used to expand a list object by adding more elements to it. Finally, we will also discuss how to insert elements at specific indices in lists.
Python Lists in a nutshell
A list is a Python data structure that is an ordered and mutable collection of items. Each element can be of any type – either a built-in Python type or a custom object while a single list can contain items of different type.
lst = ['Hello World', 10, 5.0, True]
Since list items can be of any type, this means that they can be list objects as well (i.e. nested lists). For example, a list of lists would look like the one shown below.
lst = [[1, 2, 3], [4, 5, 6]]
As mentioned, lists are ordered which means that the order the elements are specified is maintained along the way. This means that two lists are considered to be equal only and only if they contain the same elements in the same order.
>>> [10, 20] == [20, 10]
False
Each item in a list can be accessed through its index.
>>> my_lst = ['foo', 'bar]
>>> my_lst[0]
'foo'
>>> my_lst[1]
If you want to learn more about indexing and slicing over ordered collections such as lists, make sure to read the article below.
Since Python lists are mutable, they can be expanded or shrank on demand. In other words, you can insert or remove elements even into and from specific indices. In the following sections, we are going to explore the two built-in list methods that can be used to add more elements into an existing list object.
What does append() do
list.append()
is used to add an item to the end of the list.
>>> my_lst = [1, 2, 3]
>>> my_lst.append(4)
>>> my_lst
[1, 2, 3, 4]
append()
method will increase the length of the list by exactly one. This means that if the input to append
is a list of elements, then the list itself will be appended and not the individual elements:
>>> my_lst = [1, 2, 3]
>>> my_lst.append([4, 5])
>>> my_lst
[1, 2, 3, [4, 5]]
What does extend() do
On the other hand, extend()
method accepts an iterable whose elements will be appended to the list. For example, if the input to extend()
is a list, it will concatenate the first list with the other list by iterating over the elements of the input list. This means that the resulting list’s length will be increased with the size of the iterable passed to extend()
.
>>> my_lst = [1, 2, 3]
>>> my_lst.extend([4, 5, 6])
>>> my_lst
[1, 2, 3, 4, 5, 6]
Adding an element in a specific index
insert()
method can be used to insert a list item at a specific index.
>>> my_lst = [1, 2, 3]
>>> my_lst.insert(1, 4) # Add integer 4 to index 1
>>> my_lst
[1, 4, 2, 3]
As with append()
method, insert()
will increase the length of list by only one.
>>> my_lst = [1, 2, 3]
>>> my_lst.insert(1, [1, 2])
>>> my_lst
[1, [1, 2], 2, 3]
Final Thoughts
In today’s article we discussed about the main characteristics of Python Lists which are ordered and mutable collections of items. We discussed how one can add more elements into an existing list object through append()
and extend()
. The former will add the given element to the end of the list by increasing the length of list by 1. The latter will iterate over the given input and append each individual element to the list which means. Finally, we explored how to insert an element at a specific index of a list using the insert()
method.