How to Choose Between append() and extend() in Python
Both append() and extend() add elements to the end of a list, but they handle input data differently. Confusing them leads to unexpected nested structures or exploded strings.
How append() Works
The append() method treats input as a single object, adding it as one element:
nums = [1, 2]
nums.append(3)
print(nums) # [1, 2, 3]
nums.append([4, 5])
print(nums) # [1, 2, 3, [4, 5]] (a nested list!)
How extend() Works
The extend() method iterates over input and adds each element individually:
nums = [1, 2]
nums.extend([3, 4])
print(nums) # [1, 2, 3, 4] (a flat list)
nums.extend([5])
print(nums) # [1, 2, 3, 4, 5]
The String Pitfall
Since strings are iterable, extend() splits them into characters:
# Unexpected behavior
words = []
words.extend("Cat")
print(words) # ['C', 'a', 't'] ❌
# Correct approach
words = []
words.append("Cat")
print(words) # ['Cat'] ✅
# Adding multiple strings
words.extend(["Dog", "Bird"])
print(words) # ['Cat', 'Dog', 'Bird'] ✅
Output:
['C', 'a', 't']
['Cat']
['Cat', 'Dog', 'Bird']
warning
Always use append() for adding individual strings. Using extend() with a string will break it into separate characters.
Visual Comparison
data = [1, 2]
# append: adds ONE item (the list itself)
data.append([3, 4])
# [1, 2, [3, 4]]
# └─────┘ single element
# extend: adds EACH item from the iterable
data = [1, 2]
data.extend([3, 4])
# [1, 2, 3, 4]
# └──┴── two elements
Quick Reference
| Scenario | Method | Example |
|---|---|---|
| Add single value | append() | nums.append(5) |
| Add single string | append() | words.append("hello") |
| Merge two lists | extend() | list1.extend(list2) |
| Add from generator | extend() | nums.extend(range(5)) |
Summary
append(x): Addsxas a single element, regardless of typeextend(iterable): Unpacks the iterable and adds each element
When in doubt, ask yourself: "Do I want to add this thing, or add each item inside this thing?"