Adds an item to the end of a list.
Synopsis
items.append(item)
Description
append changes the list itself. Start with an empty list, [], and grow it.
Parameters
| Name | Meaning |
|---|---|
item |
The value to add. |
Return value
Nothing (None). The list itself changes.
Example
names = []
names.append("Ada")
names.append("Grace")
print(names)
print(len(names))
It prints:
['Ada', 'Grace']
2
Common mistake
names = ["Ada"]
names = names.append("Grace")
print(names)
It prints:
None
append returns None, so this throws the list away. Write names.append("Grace") on its own line.
See also
Last updated 2 weeks ago