ZetCode

Python add list element

last modified October 30, 2026 Python 3.15

In this article we show how to add elements to a list in Python.

Python list

In Python, a list is an ordered collection of values. A list can contain values of different types. A list is a mutable container, which means that we can add values, delete values, or modify existing values.

The values of a list are called items or elements of the list. A list can contain duplicate elements. Each occurrence is considered a distinct item.

There are several ways of adding elements to a list in Python:

The append, insert, and extend methods, the += operator, and slice assignment modify the list in place. The + operator and unpacking create a new list.

Python add list element with append

The append method adds an item to the end of the list.

append_method.py
vals = [1, 2, 3, 4]

vals.append(5)
vals.append(6)

print(vals)

We have a list of integer values. We add new elements to the list with append.

vals.append(5)
vals.append(6)

Two values are added at the end of the list. The append method works in amortized constant time, so it is the fastest way of growing a list.

$ python append_method.py
[1, 2, 3, 4, 5, 6]

The append method adds a single object

The append method always adds exactly one element. If we pass it a list, the whole list becomes one element of the original list.

append_vs_extend.py
a = [1, 2, 3]
b = [1, 2, 3]

a.append([4, 5])
b.extend([4, 5])

print(a, len(a))
print(b, len(b))

We pass the same list to append and to extend.

a.append([4, 5])
b.extend([4, 5])

The append method creates a nested list, while extend adds the items one by one. The lengths of the two lists differ.

$ python append_vs_extend.py
[1, 2, 3, [4, 5]] 4
[1, 2, 3, 4, 5] 5

Python add list element with insert

The insert method inserts an element at the specified position. The first argument is the index of the element before which to insert.

insert_method.py
vals = [1, 2, 3, 4]

vals.insert(0, -1)
vals.insert(1, 0)
vals.insert(len(vals), 5)
vals.insert(-1, 4.5)
vals.insert(100, 6)

print(vals)

In the example, we insert five new elements into a list.

vals.insert(0, -1)

We insert the -1 value at the beginning of the list. Python lists are indexed from 0.

vals.insert(len(vals), 5)

We insert the 5 value at the end of the list. This is equivalent to append. To get the length of the list, we use the built-in len function.

vals.insert(-1, 4.5)

Negative indexes count from the end. Index -1 inserts the value before the last element, not after it.

vals.insert(100, 6)

An index past the end of the list does not raise an error; the value is appended to the end.

$ python insert_method.py
[-1, 0, 1, 2, 3, 4, 4.5, 5, 6]

Python add list element with extend

The extend method appends all the items from the specified iterable. The iterable can be another list, a tuple, a range, a generator, or any other iterable object.

extend_method.py
vals = [1, 2, 3]

vals.extend([4, 5])
vals.extend((6, 7))
vals.extend(range(8, 10))
vals.extend(x * 10 for x in range(1, 3))

print(vals)

letters = ['a']
letters.extend('bcd')
print(letters)

We extend the list with items from a list, a tuple, a range, and a generator expression.

letters = ['a']
letters.extend('bcd')

A string is an iterable of characters, so extend adds each character as a separate element. To add the whole string, we would use append.

$ python extend_method.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20]
['a', 'b', 'c', 'd']

The + and += operators

The + operator concatenates two lists and returns a new list. The += operator extends the list in place, like extend. The difference matters when other variables refer to the same list.

plus_operator.py
vals = [1, 2, 3]
alias = vals

vals = vals + [4]
print(vals, alias)

vals = [1, 2, 3]
alias = vals

vals += [4]
print(vals, alias)

The alias variable refers to the same list object as vals.

vals = vals + [4]

The + operator builds a new list and binds it to vals. The original list, still referenced by alias, is unchanged.

vals += [4]

The += operator modifies the existing list, so the change is visible through alias too. Unlike +, which requires a list on both sides, += accepts any iterable.

$ python plus_operator.py
[1, 2, 3, 4] [1, 2, 3]
[1, 2, 3, 4] [1, 2, 3, 4]

Python add list element with slice assignment

We can add new elements by assigning an iterable to an empty slice. An empty slice at the end, at the beginning, or in the middle of the list determines where the elements go.

slice_assignment.py
vals = [1, 2, 3, 4]

vals[len(vals):] = [5, 6]
print(vals)

vals[:0] = [-1, 0]
print(vals)

vals[3:3] = [1.5, 1.75]
print(vals)

The value on the right side of the assignment must be an iterable.

vals[len(vals):] = [5, 6]

The slice starting at the end of the list is empty; the assignment appends the values.

vals[:0] = [-1, 0]

The empty slice at the beginning inserts the values at the front.

vals[3:3] = [1.5, 1.75]

The empty slice 3:3 inserts two values before the element at index 3. Unlike insert, slice assignment can insert several values at once.

$ python slice_assignment.py
[1, 2, 3, 4, 5, 6]
[-1, 0, 1, 2, 3, 4, 5, 6]
[-1, 0, 1, 1.5, 1.75, 2, 3, 4, 5, 6]

Adding elements with unpacking

The * unpacking operator inside a list display creates a new list from existing iterables and individual values.

unpacking.py
vals = [1, 2, 3]
more = (5, 6)

new_vals = [*vals, 4, *more]
print(new_vals)

new_vals = [0, *vals]
print(new_vals)
print(vals)

We build new lists from the vals list, the more tuple, and additional values.

new_vals = [*vals, 4, *more]

The items of vals and more are unpacked into the new list, with 4 placed between them.

new_vals = [0, *vals]

Prepending is easy with unpacking. The original list is not modified.

$ python unpacking.py
[1, 2, 3, 4, 5, 6]
[0, 1, 2, 3]
[1, 2, 3]

Adding elements in a loop

A common pattern is to start with an empty list and add elements to it in a loop. Often, the loop can be replaced with a list comprehension.

loop_append.py
words = ['sky', 'cup', 'forest', 'war', 'cloud', 'rock']

long_words = []

for word in words:
    if len(word) > 3:
        long_words.append(word.upper())

print(long_words)

long_words = [word.upper() for word in words if len(word) > 3]
print(long_words)

We select words longer than three characters and add their uppercase forms to a new list.

long_words = [word.upper() for word in words if len(word) > 3]

The list comprehension produces the same list as the loop with append in a single expression. See Python list comprehensions for more.

$ python loop_append.py
['FOREST', 'CLOUD', 'ROCK']
['FOREST', 'CLOUD', 'ROCK']

The append method returns None

The methods that modify a list in place return None. Assigning their result back to the variable is a common mistake.

append_returns_none.py
vals = [1, 2, 3]

res = vals.append(4)
print(res)
print(vals)

vals = vals.append(5)
print(vals)

The append method modifies the list and returns None.

vals = vals.append(5)

This line adds 5 to the list and then replaces the list with None. The list is lost. The same applies to insert and extend.

$ python append_returns_none.py
None
[1, 2, 3, 4]
None

Adding to the front of a list

A list stores its elements in a contiguous array. Inserting at the front shifts all existing elements by one position, so insert(0, x) takes linear time. When we need to add elements to both ends, a deque is a better choice.

insert_front.py
import timeit
from collections import deque

n = 100_000


def list_append():
    vals = []
    for i in range(n):
        vals.append(i)


def list_insert_front():
    vals = []
    for i in range(n):
        vals.insert(0, i)


def deque_appendleft():
    vals = deque()
    for i in range(n):
        vals.appendleft(i)


for fn in (list_append, list_insert_front, deque_appendleft):
    t = timeit.timeit(fn, number=1)
    print(f'{fn.__name__:18} {t:.4f} s')

We add 100,000 elements to a list with append, to a list with insert(0, x), and to a deque with appendleft. The timeit module measures the time.

vals.appendleft(i)

The deque from the collections module adds elements to both ends in constant time. See Python deque.

$ python insert_front.py
list_append        0.0036 s
list_insert_front  0.8784 s
deque_appendleft   0.0036 s

Source

Python data structures - Python tutorial

In this article we have shown how to add elements to a list in Python.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.