Explanatory Question
What is python list append()
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.
In this tutorial, we will learn about the Python list append() method with the help of examples.
The append() method adds an item to the end of the list.
currencies = ['Dollar', 'Euro', 'Pound'] # append 'Yen' to the list currencies.append('Yen') print(currencies) # Output: ['Dollar', 'Euro', 'Pound', 'Yen']
The syntax of the append() method is:
list.append(item)
The method takes a single argument
item - an item (number, string, list etc.) to be added at the end of the list.
The method doesn't return any value (returns None).
# animals list animals = ['cat', 'dog', 'rabbit'] # Add 'guinea pig' to the list animals.append('guinea pig') print('Updated animals list: ', animals)
Updated animals list: ['cat', 'dog', 'rabbit', 'guinea pig']
# animals list animals = ['cat', 'dog', 'rabbit'] # list of wild animals wild_animals = ['tiger', 'fox'] # appending wild_animals list to animals animals.append(wild_animals) print('Updated animals list: ', animals)
Updated animals list: ['cat', 'dog', 'rabbit', ['tiger', 'fox']]
In the program, a single item (wild_animals list) is added to the animals list.
First read the answer fully, then try to explain it in your own words. After that, open a few related questions and compare the concepts. This method helps you remember the topic for a longer time and improves exam preparation.