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 List copy()
Python String istitle()
Python Program to Find Factorial of Number Using Recursion
Python Program to Concatenate Two Lists
Python Program to Print the Fibonacci sequence
Python Program to Find Numbers Divisible by Another Number
Python strftime()
Java List UnsupportedOperationException
Python String isidentifier()
Python String center()
Python Program to Find Sum of Natural Numbers Using Recursion
Python String strip()
Python Program to Count the Occurrence of an Item in a List
Python Program to Check Whether a String is Palindrome or Not
Python Program to Shuffle Deck of Cards
Python String format()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Display Fibonacci Sequence Using Recursion
Python range()
Python List count()
Python Shallow Copy and Deep Copy
Python Program to Check If Two Strings are Anagram
Python hasattr()
Deep Learning with Python - Francois Chollet
Using a List of Values in a JdbcTemplate IN Clause
Python Program to Convert String to Datetime
Python Program to Count the Number of Digits Present In a Number
Python Program to Count the Number of Each Vowel
Python Dictionary clear()
Python Program to Check If a List is Empty
Python callable()
Python Global Keyword