How to Insert an Element at the End of a List in Python

Problem Formulation

Given a list and an element. How to insert the element at the last position of the list?

How to Insert an Element at the End of a List in Python

Two Solutions

There are two main ways to insert an element at the end of a given list.

  • Use list.append(element) to add the element to the end of the list. This is the most idiomatic approach.
  • Use 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 lst.

  • 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:

Runtime Complexity append() vs insert().

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:

Index Overshooting

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

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]

The element '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_1 and 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]

Thus, the 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.

πŸ‘‰ Recommended Tutorial: How to Insert at the First Position of a List in Python?

Programmer Humor

❓ Question: Why do programmers always mix up Halloween and Christmas?
❗ Answer: Because Oct 31 equals Dec 25.

(If you didn’t get this, read our articles on the oct() and int() Python built-in functions!)