Python List append() - Append Items to List
The append()
method adds a new item at the end of the list.
Syntax:
list.append(item)
Parameters:
item: An element (string, number, object etc.) to be added to the list.
Return Value:
Returns None.
The following adds an element to the end of the list.
city = 'Delhi'
cities = ['Mumbai', 'London', 'Paris', 'New York']
cities.append(city)
print(cities)
['Mumbai', 'London', 'Paris', 'New York', 'Delhi']
The following adds a list to the end of the list.
otherCities = ['Delhi','Chennai']
cities = ['Mumbai', 'London', 'Paris', 'New York']
cities.append(otherCities)
print(cities)
['Mumbai', 'London', 'Paris', 'New York', ['Delhi', 'Chennai']]
In the above example, the otherCities
list is added to the list cities
as a single item.
Any type of variable can be added to the list.
number = 1
cities = ['Mumbai', 'London', 'Paris', 'New York']
cities.append(number)
print(cities)
['Mumbai', 'London', 'Paris', 'New York', 1]