Python 3 reference · List Methods
Python List append() Method
Add one item to the end of a list, changing the list in place. Returns None.
Python 3 Since 3.0
Syntax
list.append(item)
Parameters
| Parameter | Description |
|---|---|
item |
The value to add (it is added as one item, even if it is a list). |
Return value
None.
Examples
fruits = ["apple"]
fruits.append("banana")
print(fruits)
Output
['apple', 'banana']
squares = []
for n in range(5):
squares.append(n * n)
print(squares)
Output
[0, 1, 4, 9, 16]
a = [1, 2]
a.append([3, 4]) # one nested item
print(a)
b = [1, 2]
b.extend([3, 4]) # several items
print(b)
Output
[1, 2, [3, 4]] [1, 2, 3, 4]
Common mistakes
append()returnsNone:items = items.append(x)throws your list away.- Appending a list adds it as one nested item; use
extend()to add its items.
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
list.extend()· Add every item from an iterable to the end of the list, in place. Like append() for many items at once.list.insert()· Insert an item at a given position, shifting later items to the right. Index 0 adds to the front.list.pop()· Remove and return the item at a position, the last item by default. Raises IndexError on an empty list.- Python Lists · tutorial