.append()
The .append()
method adds a single item to the end of an existing Python list. Lists in Python are mutable sequences that can store multiple items of different data types. When new elements need to be added to a list after it’s been created, the .append()
method provides a simple and efficient way to add items to the end of the list.
Syntax
list.append(item)
Parameters:
item
: An element of any data type (string, number, list, etc.) to be added to the end of the list.
Return value:
The method doesn’t return any value (returns None
). It modifies the original list in-place.
Example 1: Adding an item to a list
This example demonstrates how to add an item to the end of a list:
# Create a list of fruitsfruits = ['apple', 'banana', 'cherry']# Add 'orange' to the listfruits.append('orange')# Print the updated listprint(fruits)
This example results in the following output:
['apple', 'banana', 'cherry', 'orange']
The original list fruits
has been modified to include 'orange'
as the last element. The .append()
method added the new item directly to the end of the list.
Example 2: Adding list to a list
When appending a list to another list, the entire list is added as a single element:
# Create a list of fruitsfruits = ['apple', 'banana', 'cherry']# Create a list of berriesberries = ['strawberry', 'blueberry']# Append the berries list to the fruits listfruits.append(berries)# Print the updated listprint(fruits)
This example will generate the following output:
['apple', 'banana', 'cherry', ['strawberry', 'blueberry']]
Notice that the berries
list has been added as a single item to the fruits
list, creating a nested list structure. To add each element individually instead, the .extend()
method would be more appropriate.
Codebyte example: Demonstrating .append()
in Python
This example demonstrates how to use .append()
to add various items including strings, numbers, and lists to a Python list:
Frequently Asked Questions
1. What is .append()
and .extend()
in Python?
.append()
adds a single item to the end of a list as one element, even if that item is itself a list. .extend()
adds individual elements from an iterable (like another list) to the end of the current list. For example:
list1 = [1, 2, 3]list1.append([4, 5]) # Results in [1, 2, 3, [4, 5]]list2 = [1, 2, 3]list2.extend([4, 5]) # Results in [1, 2, 3, 4, 5]
2. When can I use append?
You can use .append()
whenever you need to add a single item to the end of an existing list. Common use cases include:
- Building a list dynamically as you collect data
- Adding user input to a list
- Accumulating results in a calculation
- Constructing a list step by step in algorithms
3. How to append a string?
Appending a string to a list works just like appending any other item. The string becomes a single element in the list:
words = ['hello', 'world']words.append('python') # Results in ['hello', 'world', 'python']
If you want to append each character of a string separately, you can use the .extend()
method instead:
letters = ['a', 'b', 'c']letters.extend('def') # Results in ['a', 'b', 'c', 'd', 'e', 'f']
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours