list.append()
Python · Lists
Theory
.append(item) adds one item to the end of a list, modifying the list in place. sales = [100, 250, 300] followed by sales.append(400) leaves sales as [100, 250, 300, 400]. The method returns None, so writing sales = sales.append(400) is a classic bug: it throws your list away and stores None.
append() always adds exactly one item, whatever that item is. Appending a list nests it as a single element: [1, 2].append([3, 4]) produces [1, 2, [3, 4]] with length 3, not 4; use extend() if you want the items added individually. Appending is fast, amortised O(1), because a list keeps extra capacity at the end and only occasionally grows its underlying array. That is why the common pattern of starting with an empty list and appending inside a loop scales well.
Why we use it
Data is often collected one piece at a time: a row read from a file, a result from an API, a value computed in a loop. append() is the standard, efficient way to grow a list as each new item arrives.
When to use it
Use append() to add one item at the end. Use extend() or += to add several items from another list, and insert() if the item must go at a specific position. If you are just transforming another list, a list comprehension is often cleaner.
How it works
- Call sales.append(400)
- Spare capacity at the end? Yes: Write into the next slot. No: Grow the array, copy, then write.
- Increase the list's length by 1
- Return None (the list itself was changed)
- print(sales) shows [100, 250, 300, 400]
Syntax
list.append(item)Example
sales = [100, 250, 300]
sales.append(400)
print(sales)Key points
- Modifies the list in place and returns None
- Never write x = x.append(...): x becomes None
- Adds exactly one item, even if that item is itself a list
- Amortised O(1): fast even for very large lists
- Use extend() to add the items of another list one by one