Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python list append: Usage, Behavior, and Performance

How Python's list.append() works, what it returns, its time complexity, common mistakes, and when to use extend() or list comprehensions.

~4 min readPython
#Python#list methods#data structures#performance
Illustration of a Python list with an element being appended at the end.

Python's list.append() method adds one element to the end of a list. It modifies the list in place, returns None, and has amortized O(1) time complexity. This makes it the standard way to build a list incrementally, but its in-place behavior and None return can be surprising, especially if you are used to chainable APIs.

The append() Method and Its Return Value

append() is a method on the list type. It takes one argument and adds that argument as a single element to the end of the list. The method returns None, not the modified list. This is a frequent point of confusion, especially for developers coming from languages where mutating methods return the original object for chaining.

python
numbers = [1, 2, 3] result = numbers.append(4) print(numbers) # [1, 2, 3, 4] print(result) # None

Because append() returns None, you cannot chain it like numbers.append(4).append(5). If you need to add more than one element, call append() once per element or use extend().

append() Mutates the List in Place

append() does not create a copy. Any variable that references the same list object will see the change. This matters when you pass a list to a function and append inside that function.

python
def add_item(items, item): items.append(item) cart = [] add_item(cart, "apple") print(cart) # ['apple']

If you need to avoid modifying the original list, make a copy first, for example new_items = items.copy(), and call append() on the new list.

Time Complexity and Memory Allocation

append() runs in amortized O(1) time. In CPython, lists are dynamic arrays: appending stores one reference at the end, and when the array is full, Python allocates a larger array and copies existing references. That occasional resize is amortized over many appends, so the average cost per operation stays constant.

Python may also overallocate list capacity to reduce the frequency of resizes. This means a list can use more memory than the exact number of elements requires. The exact overallocation strategy is an implementation detail and can change between Python versions.

Because append() stores references, appending a mutable object does not copy that object; later changes to the object are visible through the list.

For most applications, append() is the right choice for building a list incrementally. If you know the final size in advance, preallocating with [None] * n and assigning by index can avoid reallocations, but that is rarely necessary unless you are working with very large collections and have measured a bottleneck.

Common Mistake: append() vs extend()

A frequent error is using append() when you want to add multiple elements from an iterable. append() adds the iterable as a single element, while extend() adds each element individually.

python
numbers = [1, 2] more = [3, 4] numbers.append(more) print(numbers) # [1, 2, [3, 4]] numbers = [1, 2] numbers.extend(more) print(numbers) # [1, 2, 3, 4]

If you use append() with a list, you get a nested list. This is often not what you intended. extend() is the correct method when you want to merge an iterable into the existing list. The same distinction applies to strings: append("abc") adds the string as one element, while extend("abc") adds the characters 'a', 'b', and 'c' separately.

Using append() in Loops

append() is commonly used inside loops to collect results. Because it is amortized O(1), it is efficient even for large loops. A typical pattern is:

python
squares = [] for n in range(10): squares.append(n ** 2)

This is clear and idiomatic. A list comprehension is often more concise for simple transformations, but append() gives you more flexibility when the logic is complex or when you need to conditionally add elements.

python
filtered = [] for value in source: if value > 0: filtered.append(value)

For more involved loops, append() is often easier to read than a long comprehension. Prefer a comprehension or extend() with a generator expression when they make the code clearer, but do not sacrifice readability for micro-optimizations.

Appending While Iterating: Avoid It

Modifying a list while iterating over it is dangerous. If you append to a list inside a for loop that iterates over the same list, the iterator checks the list's current length at each step, so the loop can keep finding new items and grow until memory runs out.

python
# This loop can keep appending until memory runs out items = [1, 2, 3] for item in items: items.append(item)

To avoid this, iterate over a copy of the list, or collect new items in a separate list and extend the original after the loop.

python
items = [1, 2, 3] new_items = [] for item in items: if item < 3: new_items.append(item * 10) items.extend(new_items)

This pattern is safe and clear. Appending to a list while iterating over a different list is fine.

Alternatives: extend, +=, and List Comprehensions

extend() is the primary alternative when you need to add multiple elements. The += operator on a list is equivalent to extend(): it mutates the list in place and adds all elements from the right-hand iterable.

python
a = [1, 2] a += [3, 4] print(a) # [1, 2, 3, 4]

List comprehensions are a declarative way to build a new list without explicitly calling append(). They are often more readable for simple transformations, but they always create a new list, so they are not a drop-in replacement when you need to mutate an existing list.

OperationUse caseReturns a new listMutates in place
append(x)Add one element xNoYes
extend(iterable)Add every item from an iterableNoYes
+= iterableAdd every item from an iterableNoYes
List comprehensionBuild a new list from an iterableYesNo

Choose append() when you have one element to add. Use extend() or += when you have an iterable. Use a list comprehension when you are building a new list from an existing iterable and the transformation is simple enough to express in one expression.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.