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
1 2 3 4 5 6 7 8 | 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:
1 | 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
1 2 3 4 5 6 7 | # animals list animals = [ 'cat' , 'dog' , 'rabbit' ] # Add 'guinea pig' to the list animals.append( 'guinea pig' ) print ( 'Updated animals list: ' , animals) |
Output
1 | Updated animals list : [ 'cat' , 'dog' , 'rabbit' , 'guinea pig' ] |
5. Example 2: Adding List to a List
1 2 3 4 5 6 7 8 9 10 | # 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
1 | 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 repr()
Node.js vs Python for Backend Development
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python complex()
Python Program to Transpose a Matrix
Python eval()
Python map()
Python memoryview()
Python break and continue
Python str()
Python format()
Python String upper()
Python timestamp to datetime and vice-versa
Copy a List to Another List in Java
Removing all Nulls from a List in Java
Python String istitle()
Python @property decorator
Python Program to Find LCM
Python String partition()
Python Program to Convert Celsius To Fahrenheit
Python getattr()
Python callable()
Python String casefold()
Python next()
Python Deep Learning Cookbook - Indra den Bakker
Python Program to Calculate the Area of a Triangle
Python Get Current time
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Merge Two Dictionaries
Python Program to Compute all the Permutation of the String
Python Program to Check if a Number is Odd or Even
Converting a List to String in Java