Python list append() method
Add one item to the end of a list.
Syntax
list.append(item)
Changes the list in place and returns None. The most common way to build a list in a loop.
Parameters
| item | the value to add |
Return value
None
Example
Code
basket = ["apple"] basket.append("kiwi") basket.append("mango") print(basket) squares = [] for n in range(5): squares.append(n * n) print(squares)
Output
['apple', 'kiwi', 'mango'] [0, 1, 4, 9, 16]
Good to know.
xs = xs.append(1) sets xs to None. Just write xs.append(1). To add several items use extend.Related
Learn it step by step in the free lesson Lists & Tuples, with exercises that check your answers.