Python List append() Method

How can you add more elements to a given list? Use the append() method in Python. This tutorial shows you everything you need to know to help you master an essential method of the most fundamental container data type in the Python programming language.

Definition and Usage

The list.append(x) method—as the name suggests—appends element x to the end of the list.

Here’s a short example:

>>> l = []
>>> l.append(42)
>>> l
[42]
>>> l.append(21)
>>> l
[42, 21]

In the first line of the example, you create the list l. You then append the integer element 42 to the end of the list. The result is the list with one element [42]. Finally, you append the integer element 21 to the end of that list which results in the list with two elements [42, 21].

Syntax

You can call this method on each list object in Python. Here’s the syntax:

list.append(element)

Arguments

ArgumentDescription
elementThe object you want to append to the list.

Code Puzzle

Now you know the basics. Let’s deepen your understanding with a short code puzzle—can you solve it?

# Puzzle
nums = [1, 2, 3]
nums.append(nums[:])

print(len(nums))
# What's the output of this code snippet?

You can check out the solution on the Finxter app. (I know it’s tricky!)

Examples

Let’s dive into a few more examples:

>>> lst = [2, 3]
>>> lst.append(3)
>>> lst.append([1,2])
>>> lst.append((3,4))
>>> lst
[2, 3, 3, [1, 2], (3, 4)]

You can see that the append() method also allows for other objects. But be careful: you cannot append multiple elements in one method call. This will only add one new element (even if this new element is a list by itself). Instead, to add multiple elements to your list, you need to call the append() method multiple times.

Python List append() At The Beginning

What if you want to use the append() method at the beginning: you want to “append” an element just before the first element of the list.

Well, you should work on your terminology for starters. But if you insist, you can use the insert() method instead.

Here’s an example:

>>> lst = [1, 2, 3]
>>> lst.insert(0, 99)
>>> lst
[99, 1, 2, 3]

The insert(i, x) method inserts an element x at position i in the list. This way, you can insert an element to each position in the list—even at the first position. Note that if you insert an element at the first position, each subsequent element will be moved by one position. In other words, element i will move to position i+1.

Python List append() Multiple or All Elements

But what if you want to append not one but multiple elements? Or even all elements of a given iterable. Can you do it with append()? Well, let’s try:

>>> l = [1, 2, 3]
>>> l.append([4, 5, 6])
>>> l
[1, 2, 3, [4, 5, 6]]

The answer is no—you cannot append multiple elements to a list by using the append() method. But you can use another method: the extend() method:

>>> l = [1, 2, 3]
>>> l.extend([1, 2, 3])
>>> l
[1, 2, 3, 1, 2, 3]

You call the extend() method on a list object. It takes an iterable as an input argument. Then, it adds all elements of the iterable to the list, in the order of their occurrence.

Python List append() vs extend()

I shot a small video explaining the difference and which method is faster, too:

The method list.append(x) adds element x to the end of the list.

The method list.extend(iter) adds all elements in iter to the end of the list.

The difference between append() and extend() is that the former adds only one element and the latter adds a collection of elements to the list.

You can see this in the following example:

>>> l = []
>>> l.append(1)
>>> l.append(2)
>>> l
[1, 2]
>>> l.extend([3, 4, 5])
>>> l
[1, 2, 3, 4, 5]

In the code, you first add integer elements 1 and 2 to the list using two calls to the append() method. Then, you use the extend method to add the three elements 3, 4, and 5 in a single call of the extend() method.

Which method is faster — extend() vs append()?

To answer this question, I’ve written a short script that tests the runtime performance of creating large lists of increasing sizes using the extend() and the append() methods.

Our thesis is that the extend() method should be faster for larger list sizes because Python can append elements to a list in a batch rather than by calling the same method again and again.

I used my notebook with an Intel(R) Core(TM) i7-8565U 1.8GHz processor (with Turbo Boost up to 4.6 GHz) and 8 GB of RAM.

Then, I created 100 lists with both methods, extend() and append(), with sizes ranging from 10,000 elements to 1,000,000 elements. As elements, I simply incremented integer numbers by one starting from 0.

Here’s the code I used to measure and plot the results: which method is faster—append() or extend()?

import time


def list_by_append(n):
    '''Creates a list & appends n elements'''
    lst = []
    for i in range(n):
        lst.append(n)
    return lst


def list_by_extend(n):
    '''Creates a list & extends it with n elements'''
    lst = []
    lst.extend(range(n))
    return lst


# Compare runtime of both methods
list_sizes = [i * 10000 for i in range(100)]
append_runtimes = []
extend_runtimes = []

for size in list_sizes:

    # Get time stamps
    time_0 = time.time()
    list_by_append(size)
    time_1 = time.time()
    list_by_extend(size)
    time_2 = time.time()

    # Calculate runtimes
    append_runtimes.append((size, time_1 - time_0))
    extend_runtimes.append((size, time_2 - time_1))


# Plot everything
import matplotlib.pyplot as plt
import numpy as np

append_runtimes = np.array(append_runtimes)
extend_runtimes = np.array(extend_runtimes)

print(append_runtimes)
print(extend_runtimes)

plt.plot(append_runtimes[:,0], append_runtimes[:,1], label='append()')
plt.plot(extend_runtimes[:,0], extend_runtimes[:,1], label='extend()')

plt.xlabel('list size')
plt.ylabel('runtime (seconds)')

plt.legend()
plt.savefig('append_vs_extend.jpg')
plt.show()

The code consists of three high-level parts:

  • In the first part of the code, you define two functions list_by_append(n) and list_by_extend(n) that take as input argument an integer list size n and create lists of successively increasing integer elements using the append() and extend() methods, respectively.
  • In the second part of the code, you compare the runtime of both functions using 100 different values for the list size n.
  • In the third part of the code, you plot everything using the Python matplotlib library.

Here’s the resulting plot that compares the runtime of the two methods append() vs extend(). On the x axis, you can see the list size from 0 to 1,000,000 elements. On the y axis, you can see the runtime in seconds needed to execute the respective functions.

The resulting plot shows that both methods are extremely fast for a few tens of thousands of elements. In fact, they are so fast that the time() function of the time module cannot capture the elapsed time.

But as you increase the size of the lists to hundreds of thousands of elements, the extend() method starts to win:

For large lists with one million elements, the runtime of the extend() method is 60% faster than the runtime of the append() method.

The reason is the already mentioned batching of individual append operations.

However, the effect only plays out for very large lists. For small lists, you can choose either method. Well, for clarity of your code, it would still make sense to prefer extend() over append() if you need to add a bunch of elements rather than only a single element.

Python List append() vs insert()

The difference between the append() and the insert() method is the following:

  • the append(x) method adds new element x to the end of the list, and
  • the insert(i, x) method adds new element x at position i in the list. It shifts all subsequent elements one position to the right.

Here’s an example showing both append() and insert() methods in action:

>>> l = [1, 2, 3]
>>> l.append(99)
>>> l
[1, 2, 3, 99]
>>> l.insert(2, 42)
>>> l
[1, 2, 42, 3, 99]

Both methods help you add new elements to the list. But you may ask:

Which is faster, append() or insert()?

All things being equal, the append() method is significantly faster than the insert() method.

Here’s a small script that shows that the append() method has a huge performance advantage over the insert() method when creating a list with 100,000 elements.

import time

l1 = []
l2 = []

t1 = time.time()

for i in range(100000):
    l1.append(i)

t2 = time.time()

for i in range(100000):
    l2.insert(0,i)

t3 = time.time()


print("append(): " + str(t2 - t1) + " seconds")
print("insert(): " + str(t3 - t2) + " seconds")

# OUTPUT:
# append(): 0.015607357025146484 seconds
# insert(): 1.5420396327972412 seconds

The experiments were performed on my notebook with an Intel(R) Core(TM) i7-8565U 1.8GHz processor (with Turbo Boost up to 4.6 GHz) and 8 GB of RAM.

Python List append() vs concatenate

So you have two or more lists and you want to glue them together. This is called list concatenation. How can you do that?

These are six ways of concatenating lists (detailed tutorial here):

  1. List concatenation operator +
  2. List append() method
  3. List extend() method
  4. Asterisk operator *
  5. Itertools.chain()
  6. List comprehension
a = [1, 2, 3]
b = [4, 5, 6]

# 1. List concatenation operator +
l_1 = a + b

# 2. List append() method
l_2 = []

for el in a:
    l_2.append(el)
    
for el in b:
    l_2.append(el)


# 3. List extend() method
l_3 = []
l_3.extend(a)
l_3.extend(b)

# 4. Asterisk operator *
l_4 = [*a, *b]

# 5. Itertools.chain()
import itertools
l_5 = list(itertools.chain(a, b))

# 6. List comprehension
l_6 = [el for lst in (a, b) for el in lst]

Output:

'''
l_1 --> [1, 2, 3, 4, 5, 6]
l_2 --> [1, 2, 3, 4, 5, 6]
l_3 --> [1, 2, 3, 4, 5, 6]
l_4 --> [1, 2, 3, 4, 5, 6]
l_5 --> [1, 2, 3, 4, 5, 6]
l_6 --> [1, 2, 3, 4, 5, 6]
'''

What’s the best way to concatenate two lists?

If you’re busy, you may want to know the best answer immediately. Here it is:

To concatenate two lists l1, l2, use the l1.extend(l2) method which is the fastest and the most readable.

To concatenate more than two lists, use the unpacking (asterisk) operator [*l1, *l2, ..., *ln].

However, you should avoid using the append() method for list concatenation because it’s neither very efficient nor concise and readable.

Python List append() If Not Exists

A common question is the following:

How can you add or append an element to a list, but only if it doesn’t already exist in the list?

When ignoring any performance issues, the answer is simple: use an if condition in combination with the membership operation element in list and only append() the element if the result is False. As an alternative, you can also use the negative membership operation element not in list and add the element if the result is True.

Example: Say, you want to add all elements between 0 and 9 to a list of three elements. But you don’t want any duplicates. Here’s how you can do this:

lst = [1, 2, 3]
for element in range(10):
    if element not in lst:
        lst.append(element)	

Resulting list:

[1, 2, 3, 0, 4, 5, 6, 7, 8, 9]

You add all elements between 0 and 9 to the list but only if they aren’t already present. Thus, the resulting list doesn’t contain duplicates.

But there’s a problem: this method is highly inefficient!

In each loop iteration, the snippet element not in lst searches the whole list for the current element. For a list with n elements, this results in n comparisons, per iteration. As you have n iterations, the runtime complexity of this code snippet is quadratic in the number of elements.

Can you do better?

Sure, but you need to look beyond the list data type: Python sets are the right abstraction here. If you need to refresh your basic understanding of the set data type, check out my detailed set tutorial (with Harry Potter examples) on the Finxter blog.

Why are Python sets great for this? Because they don’t allow any duplicates per design: a set is a unique collection of unordered elements. And the runtime complexity of the membership operation is not linear in the number of elements (as it’s the case for lists) but constant!

Example: Say, you want to add all elements between 0 and 9 to a set of three elements. But you don’t want any duplicates. Here’s how you can do this with sets:

s = {1, 2, 3}
for element in range(10):
    s.add(element)

print(s)

Resulting set:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

The set doesn’t allow for duplicate entries so the elements 1, 2, and 3 are not added twice to the set.

You can even make this code more concise:

s = {1, 2, 3}
s = s.union(range(10))

print(s)

Output:

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

The union method creates a new set that consists of all elements in both operands.

Now, you may want to have a list as a result and not a set. The solution is simple: convert the resulting set to a list by using the list(set) conversion method. This has linear runtime complexity and if you call it only once, it doesn’t change the overall runtime complexity of the code snippet (it remains linear in the number of set elements).

Problem: what if you want to maintain the order information and still add all elements that are not already in the list?

The problem with the previous approach is that by converting the list to a set, the order of the list is lost. In this case, I’d advise you to do the following: use two data structures, a list and a set. You use the list to add new elements and keep the order information. You use the set to check membership (constant rather than linear runtime complexity). Here’s the code:

lst = [1, 2, 3]
s = set(lst)

for element in range(10):
    if element not in s:
        s.add(element)
        lst.append(element)
        
print(lst)

Resulting list:

[1, 2, 3, 0, 4, 5, 6, 7, 8, 9]

You can see that the resulting list doesn’t contain any duplicates but the order information is maintained. At the same time, the runtime complexity of the code is linear because each loop iteration can be completed in constant time.

The trade-off is that you have to maintain two data structures which results in double the memory overhead. This nicely demonstrates the common inverse relationship between memory and runtime overhead.

Python List append() Return New List

If you use the lst.append(element) operation, you add the element to the existing list lst. But what if you want to create a new list where the element was added?

The answer is simply to use the list concatenation operation lst + [element] which creates a new list each time it is used. The original list lst will not be affected by the list concatenation operation.

Here’s an example that shows that the append() method only modifies an existing list:

>>> lst_1 = [1, 2, 3]
>>> lst_2 = lst_1.append(4)
>>> lst_1
[1, 2, 3, 4]

And here’s the example that shows how to create a new list as you add a new element 4 to a list:

>>> lst_3 = [1, 2, 3]
>>> lst_4 = lst_3 + [4]
>>> lst_3
[1, 2, 3]

By using the list concatenation operation, you can create a new list rather than appending the element to an existing list.

Python List append() Time Complexity, Memory, and Efficiency

Time Complexity: The append() method has constant time complexity O(1). Adding one element to the list requires only a constant number of operations—no matter the size of the list.

Space Complexity: The append() method has constant space complexity O(1). The operation itself needs only a constant number of bytes for the involved temporary variables. The memory overhead does not depend on the size of the list. Note that the list itself does have linear space complexity: you need O(n) bytes to represent n elements in the list.

Efficiency Considerations: The append() method is as efficient as possible. In terms of the asymptotic behavior of the time complexity, there’s no way to improve upon the append() method—even if you’d use other data structures such as sets or binary trees. However, if you need to add multiple elements to the list, you can get some constant factor improvements by using the extend() method rather than the append() method. The former takes an iterable as an argument so you can add many elements at once in a single batch. This is more efficient and can lead to 50% performance improvements in practical settings. If you’re interested in the most performant ways to add multiple elements to a list, you can see extensive performance tests in this tutorial on the Finxter blog.

Python List append() at Index

Do you want to append an element at a certain position? This is called insertion and you can do it with the list.insert(i, x) method that inserts element x at position i of the list. All subsequent elements will be shifted to the right (their index increases by one). The time complexity of the insert() method is O(1).

Here’s an example:

>>> lst = [99, 42, 101]
>>> lst.insert(2, 7)
>>> lst
[99, 42, 7, 101]

The code inserts element 7 at position 2 of the list. Element 101 previously held position 2 so it now holds position 3.

If you want to insert an element and create a new list by doing so, I’d recommend to use Python slicing. Check out this in-depth blog tutorial that’ll show you everything you need to know about slicing. You can also get your free slicing book “Coffee Break Python Slicing”.

Here’s the code that shows how to create a new list after inserting an element at a certain position:

>>> lst = [33, 44, 55]
>>> lst[:2] + [99] + lst[2:]
[33, 44, 99, 55]

Again, you’re using list concatenation to create a new list with element 99 inserted at position 2. Note that the slicing operations lst[:2] and lst[2:] create their own shallow copy of the list.

Python List append() Error

Actually, there isn’t a lot of things you can do wrong by using the append() method.

1) One common error happens when you assume that the append() method creates a new list. This is not the case: there’s no return value for the append() method. It simply appends an element to an existing list.

2) Another error can happen if you try to append an element to a list but the list is not yet created. Of course, you can only call the method if you properly initialized the list object.

3) Yet another error happens if you try to use the append() method with too many arguments. The append() method takes only one argument: the element to be appended. If you add another argument (like the position on which you’d like to append the element), you’ll get an error. Other methods such as the insert() method can handle more arguments such as the position to add an element.

Python List append() Empty Element

Do you want to add an empty element to a list to get something like this: [4, 5, , 7]? I saw this in a StackOverflow question when researching this article.

Anyways, the most natural way of accomplish this is to use the None element that exists for exactly this purpose.

Here’s an example:

>>> lst = [4, 5]
>>> lst.append(None)
>>> lst.append(7)
>>> lst
[4, 5, None, 7]

For comprehensibility, I have to say that it’s not possible to add an empty element to a list, simply because of the fact that there’s no such thing as an empty element in Python.

Python List append() Thread Safe

Do you have a multiple threads that access your list at the same time? Then you need to be sure that the list operations (such as append()) are actually thread safe.

In other words: can you call the append() operation in two threads on the same list at the same time? (And can you be sure that the result is meaningful?)

The answer is yes (if you use the cPython implementation). The reason is Python’s global interpreter lock that ensures that a thread that’s currently working on it’s code will first finish its current basic Python operation as defined by the cPython implementation. Only if it terminates with this operation will the next thread be able to access the computational resource. This is ensured with a sophisticated locking scheme by the cPython implementation.

The only thing you need to know is that each basic operation in the cPython implementation is atomic. It’s executed wholly and at once before any other thread has the chance to run on the same virtual engine. Therefore, there are no race conditions. An example for such a race condition would be the following: the first thread reads a value from the list, the second threads overwrites the value, and the first thread overwrites the value again invalidating the second thread’s operation.

All cPython operations are thread-safe. But if you combine those operations into higher-level functions, those are not generally thread safe as they consist of many (possibly interleaving) operations.

Python List append() Sorted

How to insert an element into a sorted list? Well, you shouldn’t use append() in the first place because the append operation cannot insert an element at the correct position. It only appends the element to the end of the list.

Instead, you can use binary search and the list.insert(i,x) method to insert element x at position i in the list. Here’s the code for the binary search algorithm in Python:

def binary_search(lst, value):
    lo, hi = 0, len(lst)-1
    while lo <= hi:
        mid = (lo + hi) // 2
        if lst[mid] < value:
            lo = mid + 1
        elif value < lst[mid]:
            hi = mid - 1
        else:
            return mid
    return -1

    
l = [3, 6, 14, 16, 33, 55, 56, 89]
x = 56
print(binary_search(l,x))
# 6 (the index of the found element)

Please find a more detailed discussion of the binary search algorithm at my tutorial on the Finxter blog.

Python List append() Dictionary

These are the different interpretations of using the append() method with a dictionary:

  • Append a dictionary to a list.
  • Append all key value pairs from a dictionary to a list.
  • Append an element to a list stored in a dictionary.
  • Add/Append a key value pair to a dictionary.

Let’s explore them one by one:

Append a dictionary to a list. A dictionary is a Python object. So you can simply add it to a list like you would any other element:

>>> dic = {"hello": 0, "world":1}
>>> lst = [1, 2, 3]
>>> lst.append(dic)
>>> lst
[1, 2, 3, {'hello': 0, 'world': 1}]

The fourth list element is the dictionary itself.

Append all key value pairs from a dictionary to a list. Say, you’ve got a dictionary with (key, value) pairs. How can you add all of them to a given list? The answer is simple: use the extend() method with the dictionary method items().

>>> income = {"Alice": 100000, "Bob": 44000}
>>> lst = [("Carl", 22000)]
>>> lst.extend(income.items())
>>> lst
[('Carl', 22000), ('Alice', 100000), ('Bob', 44000)]

The items() method returns all key value pairs as tuples. You can master Python dictionaries by following my visual, ultimate guide on this blog.

Append an element to a list stored in a dictionary. This one is easy: retrieve the list and call the append() method on it. Here’s how:

>>> teams = {"A" : ["Alice", "Anna"], "B" : ["Bob", "Bernd"]}
>>> teams["A"].append("Atilla")
>>> teams["A"]
['Alice', 'Anna', 'Atilla']

As the list is an object, modifying this object (even if it’s “outside” the dictionary) will affect the object stored in the dictionary itself.

Add/append a key value pair to a dictionary. How can you add a (key, value) pair to a dictionary? Simply use the operation dic[key] = value.

Python List append() For Loop One Line

You’re looking for a one-line for loop to add elements to a list? This is called list comprehension and I’ve written a detailed article about it on this blog.

Here’s the quick example to add all elements from 0 to 9 to a list:

>>> [i for i in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Where to Go From Here?

The list.append(x) method adds element x to the end of the list.

You’ve learned the ins and outs of this important Python list method.

If you keep struggling with those basic Python commands and you feel stuck in your learning progress, I’ve got something for you: Python One-Liners (Amazon Link).

In the book, I’ll give you a thorough overview of critical computer science topics such as machine learning, regular expression, data science, NumPy, and Python basics—all in a single line of Python code!

Get the book from Amazon!

OFFICIAL BOOK DESCRIPTION: Python One-Liners will show readers how to perform useful tasks with one line of Python code. Following a brief Python refresher, the book covers essential advanced topics like slicing, list comprehension, broadcasting, lambda functions, algorithms, regular expressions, neural networks, logistic regression and more. Each of the 50 book sections introduces a problem to solve, walks the reader through the skills necessary to solve that problem, then provides a concise one-liner Python solution with a detailed explanation.