list.append()

Adds one item to the end of a list.

Returns
None
Changes the original
Yes
On this page

Example

#
Python
completed = ["HTML"]
completed.append("CSS")
completed.append(["Flexbox", "Grid"])

print(completed)
print(len(completed))
Resultsaved result, you can check it
['HTML', 'CSS', ['Flexbox', 'Grid']]
3

Definition and usage

#

The append(x) method adds an item to the end of the list. It changes the list in place and returns None, so do not assign its result back to the variable.

It adds exactly one item. If you pass in a list, that list ends up inside as a single nested item. To add several items at once, use extend().

Adding to the end of a list is very fast, which is why append() inside a loop is a common way to build up a list of results.

Syntax

#
Syntax
list.append(element)

Parameters

#
  • x

    any value

    The item to add.

More examples

#
A common mistake: assigning the result
Python
tasks = ["task 1"]
tasks = tasks.append("task 2")
print(tasks)
Resultsaved result, you can check it
None

Good practices

#
  • Do not write my_list = my_list.append(x). The method returns None, and you will lose the list.
  • To add several items at once, use extend() or +=.

Related entries

#

Spotted a mistake or missing an example? Write to us.