What .append() does in Python
The .append() method on Python lists adds a single element to the end of the list in-place and returns None. It is a synchronous, constant-time operation unless resizing is required, in which case the amortized cost remains O(1). Understanding when and how to use .append() helps you write clearer, more efficient code when building collections dynamically.
Syntax and basic usage
Use list.append(object) to place one item at the end of an existing list. The method modifies the original list and does not create a new list, which distinguishes it from operations like + or list.extend(). Always provide exactly one argument; passing multiple arguments or another list will nest that list as a single element rather than concatenate it.
Simple example
Initialize an empty list and add elements one at a time. This pattern is common when collecting results iteratively or processing streams of data.
- Start with an empty list:
items = [] - Append elements:
items.append("apple"),items.append("banana") - Result:
["apple", "banana"]
Performance and amortized cost
.append() runs in amortized O(1) time because Python overallocates memory for lists. Most appends execute in constant time; occasional resizing copies existing elements, but the cost averages out over many appends. This makes .append() efficient for incremental list construction compared to repeatedly creating new lists with +.
Time complexity comparison
| Operation | Time complexity | Notes |
|---|---|---|
list.append(x) |
Amortized O(1) | In-place addition to the end |
list + [x] |
O(n) | Creates a new list each time |
list.extend(iterable) |
Amortized O(k) |
Common pitfalls and misconceptions
A frequent mistake is assuming .append() returns the updated list. In reality, it returns None because it mutates the list in place. Another subtle issue arises when appending a list or another mutable object; this adds the object itself as a single element rather than merging its contents. Chaining appends in expressions like print(list.append(x)) will output None if you’re not careful.
.append()mutates the original list and returnsNone.- Appending a list nests it as one element; use
.extend()to concatenate. - Avoid using
.append()inside print statements if you expect to see the updated list.
Alternatives and when to choose them
Depending on your goals, other operations may be more appropriate than .append(). Use .extend() to add multiple elements from an iterable, + for immutable-style concatenation (creating a new list), list.insert(index, object) to place an element at a specific position, and collections.deque for fast appends and pops from both ends.
.append() vs .extend()
.append(): adds its argument as a single element..extend(): iterates over the argument and adds each element individually.
.append() vs +
.append(): in-place, O(1) amortized, modifies the original list.+: creates a new list, O(n + k), leaves originals unchanged.
Best practices
Prefer .append() when you need to grow a list incrementally in a loop or pipeline. For bulk additions, .extend() is clearer and slightly more efficient. Avoid using .append() when immutability is desired; consider building a new list or using tuples if appropriate. Keep your code readable by avoiding chained mutations and by storing the result of in-place operations only when necessary.
- Use
.append()for single-item, in-place addition. - Prefer
.extend()for multiple items or iterables. - Respect immutability needs by avoiding in-place mutations when they are not intended.