Given a list and an element. How to insert the element at the last position of the list?
There are two main ways to insert an element at the end of a given list.
list.append(element)to add the element to the end of the list. This is the most idiomatic approach.
list.insert(len(list), element)to “insert” the element to the end of the list. This is a bit odd but it is often used when determining the insertion index dynamically.
You can see both ways in action in the following minimal code example:
>>> lst = [1, 2, 3] >>> lst.append('Alice') >>> lst [1, 2, 3, 'Alice'] >>> lst.insert(len(lst), 'Bob') >>> lst [1, 2, 3, 'Alice', 'Bob']
You create the list
[1, 2, 3] and store it in the variable
- You append the element
'Alice'to the end of the list. It has now four elements
[1, 2, 3, 'Alice'].
- You insert the new element
'Bob'to the last position in the list with index
len(lst). Note that Python uses zero-based indexing so the first position has index 0. The resulting list has 5 elements and the new element is at the tail of the list.
However, both methods are not created equal. Appending an element has constant runtime complexity no matter how large the list is (O(1)). Inserting an element has linear runtime complexity and grows with growing list sizes (O(n)).
You can see this here:
Videos List insert() and append()
To dive deeper into the
list.append() method, I’d recommend you watch my full explainer video here:
To dive deeper into the very important
list.insert() method, I’d recommend you watch my full explainer video here:
In fact, you can insert an element at the end of a list by using any insertion index that is larger or equal to the length of the list. Formally,
list.insert(index, element) will append the element to the end of the list when using any
index >= len(lst).
Here are a couple of examples that insert three elements at indices that are at least the length of the list at the point in time of insertion. In these cases, the elements are merely appended to the list.
>>> lst = [1, 2, 3] >>> lst.insert(3, 'Alice') >>> lst.insert(100, 'Bob') >>> lst.insert(9999999, 'Carl') >>> lst [1, 2, 3, 'Alice', 'Bob', 'Carl']
Negative indexing is often used in list indexing schemes to count from the right instead of the left. The last position has negative index -1. However, you cannot use negative indexing and try to be too smart because the largest negative index is -1 and it would insert the element at the second last position, not the last.
The following example shows an epic fail:
>>> lst = [1, 2, 3] >>> lst.insert(-1, 'Alice') >>> lst [1, 2, 'Alice', 3]
'Alice' is now the second-last element, not the last.
“Append” using List Concatenation
Note that some people recommend to insert an element at the last position of a list like so:
>>> lst = [1, 2, 3] >>> lst = lst + ['new'] >>> lst [1, 2, 3, 'new']
While the output looks the same, this doesn’t actually solve the problem because the list concatenation operator
list_1 + list_2 creates a new list with the elements of two existing lists. The original lists remain unchanged. Only by assigning it to the variable
lst, you overwrite it. However, if another variable would point to the old list, this option based on list concatenation wouldn’t work because the old list remains unchanged.
>>> lst_1 = [1, 2, 3] >>> lst_2 = lst_1 >>> lst_2 = lst_2 + ['new']
In this example, you create two lists
lst_2 both referring to the same list object in memory. You try to append the new element to the end of the list using the problematic method. And you obtain a clash—both lists refer to different objects in memory!
>>> lst_2 [1, 2, 3, 'new'] >>> lst_1 [1, 2, 3]
list.append('new') method is superior to list concatenation to append an element at the last position in the list.
>>> lst_1 = [1, 2, 3] >>> lst_2 = lst_1 >>> lst_2.append('new') >>> lst_1 [1, 2, 3, 'new'] >>> lst_2 [1, 2, 3, 'new']
Both variables now refer to the same, changed list object.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.