PYTHON

The Python List append() Method: Syntax, Usage, and Examples

In Python, append() is a list method that adds a single element to the end of a list. This makes it a crucial tool for working with data structures like lists and tuples.

Quick Answer: How to Append to a List in Python

To add a single item to the end of a list in Python, use the .append() method. This method modifies the list in-place, meaning it changes the original list directly and does not return a new list.

Syntax:your_list.append(element_to_add)

Example:

# Start with a list of numbers
numbers = [1, 2, 3]

# Add a new number to the end
numbers.append(4)

print(numbers)  # Output: [1, 2, 3, 4]

# You can append any data type, including another list
numbers.append([5, 6])
print(numbers) # Output: [1, 2, 3, 4, [5, 6]]

If you need to add all items from another list individually (not as a nested list), use the .extend() method instead.

How to Use Python List append()

The syntax of using the append() method in the Python programming language is simple and straightforward. Here’s a basic example:

# Creating a list of strings and appending an item
fruits = ['apple', 'banana']
fruits.append('cherry')  # Adds 'cherry' to the end of the list
print(fruits)  # Outputs: ['apple', 'banana', 'cherry']

The append() method modifies the original list by adding the element to the end. It’s a built-in feature of Python that simplifies the process of dynamically managing lists. The append method works with any iterable data type, including strings, numbers, or even nested lists.

When to Use append() in Python Lists

Building Lists

When collecting or generating data in a loop, append() can add an element at a time to a list. Once you’ve created a list using square brackets ([]), you can use the append() method on it. This use case is beneficial for beginners learning how to manipulate lists in Python.

squares = []
for i in range(5):
    squares.append(i**2)  # Appends the square of i

Aggregating Results

In data processing and handling, append() is useful for aggregating results into a single list. It simplifies adding new data points to a mutable structure like a list. The parameter passed to append() can be a value or a more complex data type like a dictionary or another list.

results = []
for experiment in experiments:
    result = run_experiment(experiment)
    results.append(result)  # Collects each experiment's result

User Input Collection

append() is ideal for scenarios requiring the collection of user inputs. As an example, consider responses to survey questions or user preferences.

responses = []
while True:
    response = input("Enter your response (or 'done' to finish): ")
    if response == 'done':
        break
    responses.append(response)

Filtering Data with Conditions

The append method can also add elements to a list dynamically based on boolean conditions. This makes it useful in scenarios like filtering or sorting data.

numbers = [1, 2, 3, 4, 5]
filtered_numbers = []
for num in numbers:
    if num % 2 == 0:
        filtered_numbers.append(num)  # Appends only even numbers
print(filtered_numbers)  # Outputs: [2, 4]

Examples of Using append() in Python Lists

E-commerce Shopping Cart

In e-commerce platforms, append() can manage shopping cart functionality, allowing users to add items as they browse. This demonstrates a common use case in web applications.

shopping_cart = []
shopping_cart.append('T-shirt')  # User adds a T-shirt to the cart
shopping_cart.append('Jeans')  # User adds Jeans to the cart

Data Collection in Scientific Research

In scientific research, data points accumulate over time. In such cases, append() can add the data points in an ordered list at the time of measurement.

measurements = []
for _ in range(10):
    measurement = collect_measurement()
    measurements.append(measurement)  # Stores each new measurement

Social Media Applications

In social media applications, append() can add new posts or comments to a user's feed or comment section.

comments = []
comments.append("Great photo!")  # A user adds a new comment
comments.append("Love this!")  # Another user adds a comment

Learn More About the Python List append() Method

Appending Various Data Types

The append() method allows you to add items of any data type. This includes integers, strings, and even other lists or complex objects. Both methods are commonly used depending on whether you want to add a new list as an item or merge two lists.

# Appending different data types
numbers = [1, 2, 3]
numbers.append(4)  # Appends an integer

strings = ['hello']
strings.append('world')  # Appends a string

# Appending a list
lists = [[1, 2], [3, 4]]
lists.append([5, 6])  # Appends a list

Appending vs. Extending Lists

The append() method adds its argument as a single item to the end of a list. By contrast, the list extend() method unpacks the argument and adds each element to the list. This is useful when you need to combine two lists or add multiple elements to an existing list at once.

# Using append()
list1 = ['a', 'b', 'c']
list1.append(['d', 'e'])
print(list1)  # Outputs: ['a', 'b', 'c', ['d', 'e']]

# Using extend()
list2 = ['a', 'b', 'c']
list2.extend(['d', 'e'])
print(list2)  # Outputs: ['a', 'b', 'c', 'd', 'e']

Caveats of Using append()

Appending Strings in Python

The append() method is only available to lists. Python strings are immutable and have no append() function. To modify or concatenate strings, you need to use string concatenation or formatting methods.

# Concatenating strings with '+'
base_url1 = "http://example.com"
endpoint1 = "/api/data"
full_url1 = base_url1 + endpoint1
print(full_url1)  # Outputs: "http://example.com/api/data"

# Using format strings for concatenation
base_url2 = "http://example.com"
endpoint2 = "/api/data"
full_url2 = f"{base_url2}{endpoint2}"
print(full_url2)  # Outputs: "http://example.com/api/data"

Using List Comprehension Instead of append()

While append() is widely used, list comprehension offers a more concise and Pythonic way to generate lists. For example, when creating a new list based on conditions, list comprehension is often the preferred approach.

# Using list comprehension to generate squares
squares = [i**2 for i in range(5)]
print(squares)  # Outputs: [0, 1, 4, 9, 16]

Limitations with Tuples

Tuples are immutable, so they don’t support the append() method. Instead, you need to create a new tuple that includes the additional elements.

# Adding elements to a tuple by creating a new one
my_tuple = (1, 2, 3)
new_tuple = my_tuple + (4,)
print(new_tuple)  # Outputs: (1, 2, 3, 4

Appending Mutable Objects

When you append a mutable object, such as a dictionary, to a list, any subsequent changes to the object will also reflect in the list.

# Appending mutable objects like dictionaries
data = []
entry = {"name": "Alice"}
data.append(entry)
entry["name"] = "Bob"
print(data)  # Outputs: [{'name': 'Bob'}]

Key Takeaways for Python List append()

  • Adds One Element: .append() takes exactly one argument and adds it as a single element to the end of the list.
  • Modifies In-Place: It is an "in-place" method, which means it modifies the original list directly.
  • Returns None: The .append() method does not return the modified list or the added element; it always returns None. A common mistake is assigning the result back to the variable (e.g., my_list = my_list.append(item)), which will overwrite your list with None.
  • Use .extend() for Multiple Items: If you want to add all items from another list or iterable, use the .extend() method.
  • Lists Only: The .append() method is specific to lists and cannot be used on immutable data types like strings or tuples.
Learn to Code in Python for Free
Start learning now
button icon
To advance beyond this tutorial and learn Python by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH

Reach your coding goals faster