The append() function in python helps to add the given item to the end of the existing list.
list.append(item) #where item can be numbers, strings, dictionaries etc
The append() function takes a single parameter.
Parameter | Description | Required / Optional |
---|---|---|
item | an item to be added at the end of the list | Required |
This method doesn't return any value which means it returns none. Actually, this method updates the existing list.
# flowers list
flowers = ['Rose', 'Lotus', 'Jasmine']
# 'Sunflower' is appended to the flowers list
flowers.append('Sunflower')
# Updated flowers list
print('Updated flowers list: ', flowers )
Output:
Updated flowers list: ['Rose', 'Lotus', 'Jasmine', 'Sunflower']
# flowers list
flowers = ['Rose', 'Lotus', 'Jasmine']
# flowers list
flowers = ['Rose', 'Lotus', 'Jasmine']
# list of vegetables
vegetables = ['Tomato', 'Potato']
# appending vegetables list to the flowers list
flowers.append(vegetables)
print('Updated list: ', flowers)
Output:
Updated list: ['Rose', 'Lotus', 'Jasmine', ['Tomato', 'Potato']]