Table of Contents
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.
Example
currencies = ['Dollar', 'Euro', 'Pound']
# append 'Yen' to the list
currencies.append('Yen')
print(currencies)
# Output: ['Dollar', 'Euro', 'Pound', 'Yen']
1. Syntax of List append()
The syntax of the append() method is:
list.append(item)
2. append() Parameters
The method takes a single argument
- item – an item (number, string, list etc.) to be added at the end of the list
3. Return Value from append()
The method doesn’t return any value (returns None).
4. Example 1: Adding Element to a List
# animals list
animals = ['cat', 'dog', 'rabbit']
# Add 'guinea pig' to the list
animals.append('guinea pig')
print('Updated animals list: ', animals)
Output
Updated animals list: ['cat', 'dog', 'rabbit', 'guinea pig']
5. Example 2: Adding List to a List
# 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)
Output
Updated animals list: ['cat', 'dog', 'rabbit', ['tiger', 'fox']]
In the program, a single item (wild_animals list) is added to the animals list.
Note: If you need to add items of a list (rather than the list itself) to another list, use the extend() method.
Related posts:
Python Set issubset()
Python Program to Make a Simple Calculator
Python String rfind()
Python Get Current time
Python super()
Python List clear()
Python Program to Make a Flattened List from Nested List
Finding Max/Min of a List or Collection
Python String ljust()
Python Operators
Java – Get Random Item/Element From a List
Removing all Nulls from a List in Java
Python Dictionary setdefault()
Python min()
Python Set pop()
Removing all duplicates from a List in Java
Python round()
Python String isalpha()
Python String format()
Python Directory and Files Management
Python String startswith()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python String count()
Python String isidentifier()
Python staticmethod()
Python Program to Solve Quadratic Equation
Python Program to Find the Largest Among Three Numbers
Python Set intersection_update()
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Find the Factorial of a Number
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Find ASCII Value of Character