Adding Elements to a List
Lists are one of the most useful data structures available in Python, or really any programming language, since they're used in so many different algorithms and solutions.
Once we have created a list, often times we may need to add new elements to it, whether it be at the end, beginning, or somewhere in between. Python offers us three different methods to do so. In this article I'll be showing the differences between the append
, extend
, and insert
list methods.
Append
This method adds an element at the end of an existing list. The syntax to use it is:
a.append(x)
Here the variable a
is our list, and x
is the element to add. This expression is equivalent to a[len(a):] = [x]
.
For example, here is how to use it to place the element "y" at the end of our list, a
:
a = [1, 'x', 2]
a.append('y')
print(a)
Running this code will result in the following output:
$ python append.py
[1, 'x', 2, 'y']
Insert
This method inserts an item at a specified position within the given list. The syntax is:
a.insert(i, x)
Here the argument i
is the index of the element before which to insert the element x
. Thus, a.insert(len(a), x)
is the same thing as a.append(x)
. Although, the power of this method comes in using it to place items somewhere within the list and not at the end. If you only needed to add an element to the end of the list then append
works just fine for that, and is faster (which matters for large lists).
For example:
a = [1, 'x', 'y']
a.insert(2, 2)
print(a)
This code will result in the following output:
$ python insert.py
[1, 'x', 2, 'y']
As you can see, the element given is placed anywhere in the list that we specify. This works well when you have a list in which its items are ordered, so you can't just add your element to the end, like you would do with append
.
Extend
This method adds elements (notice its plural!) to a list by adding on all the elements of the iterable you pass to it. The resulting list is one containing all of the elements of both lists.
The syntax for using this method is:
a.extend(x)
In this case a
is our list and x
is an iterable object, such as another list. This method is equivalent to a[len(a):] = x
.
For example:
a = [1, 'x', 'y']
b = [1, 2]
a.extend(b)
print(a)
Running this code results in the following output:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
$ python extend.py
[1, 'x', 'y', 1, 2]
Notice how the two lists were combined together, one after the other.
In Python, you can also achieve the same results by doing a simple addition. So a + b
, in this case, would result in the same exact array as our script above. This is thanks to the __add__()
method being implemented in the lists, but that's out of the scope of this article.
Comparing Each Method
In order to see the different results obtained through these methods, let's do a direct comparison by running the following code:
a1 = [1, 'x', 'y']
a2 = [1, 'x', 'y']
a3 = [1, 'x', 'y']
b = [2, 3]
a1.append(b)
a2.insert(3, b)
a3.extend(b)
print(a1)
print(a2)
print(a3)
In this program, we have defined three lists with exactly the same elements. We have also defined a second list, which we append, insert and extend to each of the three similar lists previously defined. The result is as follows:
$ python all.py
[1, 'x', 'y', [2, 3]]
[1, 'x', 'y', [2, 3]]
[1, 'x', 'y', 2, 3]
As we can see, both append
and insert
add the list b
to the initial list, but as a single element, which is a list. In other words, it doesn't append each element of b
individually, but instead it appends the entire object itself.
The extend
method, on the other hand, actually adds the individual elements of list b
, as separate and unique elements of the resulting list.
This is all in agreement with what we previously saw, that is, append
and insert
add only a single element. Whereas, extend
, expands on the initial list by adding the elements of the second list at its end.
Another difference to consider is the measure of efficiency. Given how each operation works, we can pretty easily figure out the time complexity for each method. Or you can just cheat and check out the Time Complexity page on python.org's wiki page.
The time complexities are as follow:
Method | Time Complexity |
---|---|
append() |
O(1) |
insert() |
O(n) |
extend() |
O(k) |
Here "n" is the number of elements currently in the list, and "k" is the number of elements in the parameter object.
These points show that these three methods are complementary. We must choose which one to use, according to our needs:
- If we want to add an element at the end of a list, we should use
append
. It is faster and direct. - If we want to add an element somewhere within a list, we should use
insert
. It is the only option for this. - If we want to combine the elements of another iterable to our list, then we should use
extend
.
Wrapping Up
Python presents several choices to add elements into a list, all of which complement each other and have their own use-cases. In this article we presented three of those choices, how to use each, and when to use each. The choice of method you choose should be based on your needs.