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 Program to Convert Celsius To Fahrenheit
Python Program to Check Armstrong Number
Python Dictionary update()
Python Program to Convert Bytes to a String
How to get current date and time in Python?
Python bytes()
Python Global, Local and Nonlocal variables
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python String strip()
Python Program to Print Hello world!
Python Program to Find HCF or GCD
Python List sort()
Python exec()
Python Object Oriented Programming
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python String ljust()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Get Current time
Python Program to Convert Two Lists Into a Dictionary
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Compute the Power of a Number
Python enumerate()
Python Program to Differentiate Between type() and isinstance()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python any()
Python Dictionary clear()
Python Operator Overloading
Python String isdecimal()
Python Program to Merge Mails
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python float()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants