Python List Append vs Extend: What's the Real Difference?
Understand the key difference between Python's list append() and extend() methods, with practical examples and performance insights to avoid common bugs.
Advertisement
If you've been working with Python lists for any amount of time, you've probably used both append() and extend() without giving it much thought. They both add items to a list, right? Well, yes—but the way they do it is completely different, and mixing them up can lead to some nasty bugs in your code.
Let me show you what I mean with a real example from PythonSkillset's own codebase.
The Core Difference in Plain English
Here's the simplest way to think about it:
append()adds its argument as a single element to the end of the listextend()takes an iterable and adds each of its elements individually
That sounds simple enough, but the confusion happens because both methods can take lists as arguments. Let me show you what happens.
# Using append with a list
numbers = [1, 2, 3]
numbers.append([4, 5, 6])
print(numbers) # Output: [1, 2, 3, [4, 5, 6]]
Notice how the entire list [4, 5, 6] becomes a single element inside the original list. You now have a nested list.
# Using extend with a list
numbers = [1, 2, 3]
numbers.extend([4, 5, 6])
print(numbers) # Output: [1, 2, 3, 4, 5, 6]
With extend(), each element from the argument gets added individually. The result is a flat list with six elements.
When You'd Actually Use Each One
At PythonSkillset, we see developers make this mistake all the time. Someone wants to add multiple items to a list, uses append() with a list, and ends up with nested structures they didn't expect.
Here's a real scenario: Imagine you're building a list of user IDs from a database query.
# The wrong way
user_ids = [101, 102, 103]
new_users = [104, 105, 106]
user_ids.append(new_users)
print(user_ids) # [101, 102, 103, [104, 105, 106]]
Now you have a list where the last element is itself a list. If you try to loop through user_ids and do something with each ID, you'll hit a TypeError when you get to that nested list.
# The right way
user_ids = [101, 102, 103]
new_users = [104, 105, 106]
user_ids.extend(new_users)
print(user_ids) # [101, 102, 103, 104, 105, 106]
What Actually Happens Under the Hood
When you call append(), Python treats whatever you pass as a single object. It doesn't care if it's a number, a string, a list, or a dictionary—it just adds that one thing to the end of your list.
extend(), on the other hand, expects an iterable (like a list, tuple, or string). It loops through that iterable and adds each item one by one.
Here's a quick way to remember: append adds one item, extend adds many items from a collection.
A Common Mistake That Costs Time
I've seen developers at PythonSkillset spend hours debugging code because of this confusion. Here's a typical scenario:
# You want to combine two lists of customer names
old_customers = ["Alice", "Bob", "Charlie"]
new_customers = ["Diana", "Edward"]
# Wrong approach
old_customers.append(new_customers)
print(len(old_customers)) # Output: 4 (not 5!)
The developer expected 5 customers but got 4. The list new_customers was added as a single element, not as two separate entries.
When to Use Each One
Use append() when:
- You're adding a single item to a list
- You want to create a list of lists (nested structure)
- You're adding something that shouldn't be broken apart
# Adding a single number
scores = [85, 92, 78]
scores.append(95)
print(scores) # [85, 92, 78, 95]
# Building a list of coordinates
coordinates = []
coordinates.append([10, 20])
coordinates.append([30, 40])
print(coordinates) # [[10, 20], [30, 40]]
Use extend() when:
- You have a collection of items you want to merge into an existing list
- You're reading data from a file or database and want to add all results
- You want to flatten a list of lists into a single list
# Merging search results from multiple pages
all_results = []
page1 = ["item1", "item2", "item3"]
page2 = ["item4", "item5"]
all_results.extend(page1)
all_results.extend(page2)
print(all_results) # ['item1', 'item2', 'item3', 'item4', 'item5']
The Performance Angle
There's a subtle performance difference too. When you use extend(), Python knows you're adding multiple items and can optimize the memory allocation. With append() inside a loop, Python has to resize the list more often.
import time
# Using append in a loop
start = time.time()
my_list = []
for i in range(100000):
my_list.append(i)
print("append time:", time.time() - start)
# Using extend
start = time.time()
my_list = []
my_list.extend(range(100000))
print("extend time:", time.time() - start)
On most systems, extend() will be noticeably faster because it handles the memory allocation in one go rather than resizing the list 100,000 times.
What About Strings?
This is where things get interesting. Strings are iterable in Python, so extend() treats them as a sequence of characters.
# Using extend with a string
letters = ['a', 'b']
letters.extend("cd")
print(letters) # ['a', 'b', 'c', 'd']
But append() treats the string as a single object:
letters = ['a', 'b']
letters.append("cd")
print(letters) # ['a', 'b', 'cd']
This catches people off guard all the time. If you're building a list of words and accidentally use extend() with a string, you'll end up with individual characters instead of whole words.
The Practical Takeaway
Here's what I tell the junior developers at PythonSkillset:
- Use
append()when you have one thing to add, even if that thing is a list or dictionary - Use
extend()when you have a collection of things that should become separate elements
A quick mental check: if you're adding something and you want the length of your list to increase by more than 1, you probably want extend(). If you want it to increase by exactly 1, use append().
One More Thing: The + Operator
You can also combine lists using the + operator, which works like extend() but creates a new list instead of modifying the original.
list_a = [1, 2, 3]
list_b = [4, 5, 6]
combined = list_a + list_b
print(combined) # [1, 2, 3, 4, 5, 6]
print(list_a) # [1, 2, 3] (unchanged)
This is useful when you want to keep the original lists intact, but it's less memory-efficient than extend() for large lists because it creates a copy.
The Bottom Line
Think of it this way: append() is for adding a single item to your list, even if that item is itself a collection. extend() is for merging two collections into one flat list.
Next time you're working with lists in Python, take a second to ask yourself: "Do I want this as one element, or do I want to unpack it?" That simple question will save you from those head-scratching moments when your list structure doesn't look right.
Advertisement
Comments
Questions, corrections, and tips stay visible for everyone reading this page.
Join the discussion
No comments yet
Be the first to leave a note — it helps the next reader.