Skip to content

How to Add Elements to Lists in Python using Plus Operator and append() Method

Learn the performance differences between Python's + operator and .append() method for adding elements to lists, including when to use each for optimal speed and memory efficiency.

Choosing the right way to grow your lists impacts both readability and performance. While the + operator and .append() method both increase collection size, they behave differently in memory and serve distinct purposes.

Using .append(): In-Place Modification

The .append() method modifies the original list directly. It's the standard way to build lists dynamically, especially in loops.

tools = ['Git', 'Docker']

tools.append('Kubernetes')

print(tools)  # ['Git', 'Docker', 'Kubernetes']

Using the + Operator: Concatenation

The + operator joins two sequences to create an entirely new object. It doesn't modify the originals, making it ideal when immutability matters.

list_a = [1, 2]
list_b = [3, 4]

result = list_a + list_b

print(list_a)   # Still [1, 2]
print(result)   # [1, 2, 3, 4]

Performance Comparison

result = []
for i in range(10000):
    result = result + [i]  # Creates new list each iteration

result = []
for i in range(10000):
    result.append(i)  # Modifies in place

Key Differences

Feature .append() + Operator
Behavior In-place modification Creates new object
Return Value None The new list
Use Case Building lists in loops Joining distinct lists
Speed 🚀 O(1) constant 🐢 O(n+m) linear
Original List Modified Unchanged

The .extend() Alternative

Add multiple elements in-place without creating a new list:

tools = ['Git', 'Docker']

tools.extend(['Kubernetes', 'Terraform'])
print(tools)  # ['Git', 'Docker', 'Kubernetes', 'Terraform']

tools.append(['AWS', 'GCP'])
print(tools)  # ['Git', 'Docker', 'Kubernetes', 'Terraform', ['AWS', 'GCP']]

Quick Reference

Goal Method Example
Add one item .append(x) lst.append(5)
Add multiple items .extend([...]) lst.extend([5, 6])
Join two lists (new) + new = a + b
Join two lists (in-place) .extend() a.extend(b)

Summary

Use .append() for building lists dynamically because it's fast and memory-efficient. Use + when you need a new list without modifying the originals. For adding multiple elements in-place, use .extend(). Avoid list = list + [item] in loops as it creates unnecessary copies and degrades performance.

Navigation