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:
Partition a List in Java
Python RegEx
Python Set difference()
Python Dictionary keys()
Converting between an Array and a List in Java
How to Find an Element in a List with Java
Python String rstrip()
Python Global, Local and Nonlocal variables
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String partition()
Python Set remove()
Python exec()
Python Program to Find All File with .txt Extension Present Inside a Directory
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Keywords and Identifiers
Python Program to Copy a File
Python Dictionary
Python Set pop()
Python Program to Randomly Select an Element From the List
Python String format()
Python Program to Find the Factors of a Number
Python List pop()
Python super()
Remove All Occurrences of a Specific Value from a List
Python String rindex()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Program to Calculate the Area of a Triangle
Python Program Read a File Line by Line Into a List
Python List copy()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Add Two Matrices