Python
list methods
append vs extend
difference
programming tips

What is the difference between Python's list methods append and extend?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

append and extend both add data to a Python list, but they describe different intentions. One adds a single element, and the other concatenates elements from an iterable. If you mix them up, you can silently create nested lists, split strings into characters, or change the shape of your data pipeline.

How append and extend Actually Behave

append(x) adds one object to the end of the list. Python does not inspect x to decide whether to flatten it.

python
1numbers = [1, 2]
2numbers.append([3, 4])
3print(numbers)      # [1, 2, [3, 4]]
4print(len(numbers)) # 3

extend(iterable) iterates through its argument and appends each item one by one.

python
1numbers = [1, 2]
2numbers.extend([3, 4])
3print(numbers)      # [1, 2, 3, 4]
4print(len(numbers)) # 4

A practical memory aid is this: if you want list length to grow by exactly one, use append. If you want to merge another sequence into the list, use extend.

Input Types and Why They Matter

The most common confusion is that extend accepts any iterable, not just lists. That includes tuples, sets, generators, and strings.

python
1items = ["start"]
2items.extend(("a", "b"))
3print(items)  # ['start', 'a', 'b']
4
5items.extend("cd")
6print(items)  # ['start', 'a', 'b', 'c', 'd']

The second call may surprise people. A string is an iterable of characters, so extend adds each character separately. If you wanted the whole string as one element, append("cd") is correct.

Generators also work with extend, which is useful when streaming values.

python
1def even_numbers(limit):
2    for n in range(limit):
3        if n % 2 == 0:
4            yield n
5
6result = [1]
7result.extend(even_numbers(8))
8print(result)  # [1, 0, 2, 4, 6]

Mutation and Return Values

Both methods mutate the list in place and return None. This is important because method chaining fails silently if you expect a list back.

python
1data = [10]
2ret1 = data.append(20)
3ret2 = data.extend([30, 40])
4
5print(data)  # [10, 20, 30, 40]
6print(ret1)  # None
7print(ret2)  # None

Because these methods mutate in place, all references to the same list see the change.

python
1a = [1, 2]
2b = a
3
4a.extend([3])
5print(a)  # [1, 2, 3]
6print(b)  # [1, 2, 3]

If shared mutation is risky in your code path, create a copy first by using a.copy().

Choosing Clearly in Real Code

In production code, selecting the right method is mostly about preserving data shape.

Use append when storing composite objects like records, rows, or sublists.

python
1rows = []
2rows.append(["alice", 95])
3rows.append(["bob", 88])
4print(rows)  # [['alice', 95], ['bob', 88]]

Use extend when flattening one level or combining two sequences.

python
1combined = [1, 2]
2incoming = [3, 4, 5]
3combined.extend(incoming)
4print(combined)  # [1, 2, 3, 4, 5]

Also note that list_a += list_b is effectively the same idea as extend for lists and mutates in place.

python
1left = [1, 2]
2right = [3, 4]
3left += right
4print(left)  # [1, 2, 3, 4]

If your team reviews many data-transform functions, clear intent matters more than micro-optimization. A readable append or extend call is easier to maintain than clever one-liners.

Performance Notes Without Overstating

For bulk addition, one extend call is usually cleaner and often faster than a loop that repeatedly calls append.

python
source = list(range(10000))
target = []
target.extend(source)

That said, the biggest wins usually come from algorithm choices, not tiny method differences. Measure with realistic input sizes before optimizing deeply.

Common Pitfalls

  • Using append when you intended concatenation, which creates unwanted nested lists.
  • Using extend on a string and accidentally splitting it into characters.
  • Expecting either method to return a new list value.
  • Mutating a list that is shared through multiple references.
  • Flattening too aggressively with extend and losing intentional structure.

Summary

  • append adds one object as one element.
  • extend consumes an iterable and adds each item.
  • Strings are iterables, so extend on text adds characters.
  • Both methods mutate in place and return None.
  • Choose based on data shape: preserve structure with append, merge sequences with extend.

Course illustration
Course illustration

All Rights Reserved.