PythonLists and tuples
list. append()
Adds one item to the end of a list.
- Returns
None- Changes the original
- Yes
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
#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 returnsNone, and you will lose the list. - To add several items at once, use
extend()or+=.
Related entries
#- append() vs extend()append() adds exactly one element to the end of a list, even if that element is itself a list, while extend() appends every element of the given collection in turn.
- list.extend()Adds all the items of another collection to the end of a list.
- list.insert()Inserts an item at a chosen position in a list.
- list.pop()Removes the item at the given index (the last one by default) and returns it.
- list()Creates a list, either empty or from the items of another iterable.
Spotted a mistake or missing an example? Write to us.