Back to Blog
Python

Python append vs extend: Differences and Use Cases

python append vs extend: Understand how Python's list append and extend methods differ, when to use each, and how they affect performance and memory.

PythonList MethodsappendextendData StructuresCode Efficiency
Illustration comparing Python list append and extend operations, showing a single item added versus multiple items merged.

In Python, list.append() and list.extend() are two methods that both add elements to a list, but they behave in fundamentally different ways. The choice between python append vs extend affects how the list grows, how arguments are processed, and how the code reads. Understanding the distinction is essential for writing correct and efficient Python code.

What append and extend Do

append() adds a single object to the end of a list. The object is inserted as-is, regardless of its type. extend() iterates over the provided argument and adds each element from that iterable to the list individually.

numbers = [1, 2, 3] numbers.append([4, 5]) print(numbers) # [1, 2, 3, [4, 5]] letters = ['a', 'b'] letters.extend(['c', 'd']) print(letters) # ['a', 'b', 'c', 'd']

The first example places the entire list [4, 5] as a single element at the end. The second example unpacks the iterable and appends each item separately. This is the core behavioral difference.

In-Place Operation and Return Value

Both methods modify the list in place and return None. They do not create a new list. This is a common source of bugs when developers expect a return value and assign it to a variable.

result = [1, 2].append(3) print(result) # None

Because the operation is in-place, the original list is updated. If you need a new list, you must copy the original first or use concatenation (+), which returns a new list. The in-place nature also means both methods are efficient when you want to avoid allocating a second list.

Argument Handling: Single Element vs Iterable

append() takes exactly one argument and adds it as a single element. extend() takes an iterable and adds each of its elements. This distinction becomes critical when working with strings, tuples, or other iterables.

word = 'cat' list_with_string = [] list_with_string.append(word) print(list_with_string) # ['cat'] list_with_chars = [] list_with_chars.extend(word) print(list_with_chars) # ['c', 'a', 't']

append('cat') stores the whole string as one element. extend('cat') treats the string as a sequence of characters and adds each character. If you intend to add a string as a single item, use append. If you want to split it into characters, use extend.

Practical Examples and Common Mistakes

A frequent mistake is using append when extend is needed, or vice versa. Consider building a list from multiple iterables:

# Wrong: appends the whole list as one element all_items = [] for part in [[1, 2], [3, 4]]: all_items.append(part) print(all_items) # [[1, 2], [3, 4]] # Correct: extends with each element all_items = [] for part in [[1, 2], [3, 4]]: all_items.extend(part) print(all_items) # [1, 2, 3, 4]

Another common issue arises when using extend with a generator. extend consumes the generator immediately, so you cannot reuse it later. If you need to reuse the data, convert it to a list first.

Performance and Memory Considerations

Both methods have amortized O(1) complexity for adding a single element. append adds one element; extend adds k elements where k is the length of the iterable. The performance difference is proportional to the number of elements added, not the method itself.

extend can be more efficient than calling append in a loop because the iteration happens in C code rather than in Python bytecode. For example:

# Loop with append new_list = [] for item in iterable: new_list.append(item) # Single extend new_list = [] new_list.extend(iterable)

The extend version is usually faster because it avoids the Python-level loop overhead. However, if the iterable is already a list, extend may still need to resize the target list multiple times. Python's list allocation strategy handles this with overallocation, so the cost is amortized.

Memory usage also differs. append adds a reference to the existing object; it does not copy the object. extend adds references to each element from the iterable. Neither method performs a deep copy. If the iterable holds mutable objects, both the original and the list share the same object references.

When to Use append vs extend

Use append when you need to add a single item, regardless of whether that item is a number, string, list, or dictionary. Use extend when you have an iterable and want to merge its elements into the list.

MethodArgumentResultTypical Use Case
appendOne objectAdds the object as a single elementAdding a value to a list, e.g., collecting results
extendOne iterableAdds each element from the iterableMerging lists, flattening one level, adding generator output

A concrete decision rule: if you write list.append(x) and x is a list, you get a nested list. If you write list.extend(x) and x is a list, you get a flat list. Choose based on the desired structure.

Advanced Usage: Nested Lists and Generators

extend works with any iterable, including generators, sets, and custom iterable objects. This makes it useful for building lists from data streams without intermediate lists.

def squares(n): for i in range(n): yield i * i result = [] result.extend(squares(4)) print(result) # [0, 1, 4, 9]

append is often used to build lists of lists, such as when grouping data:

groups = [] for group in data: groups.append(group) # group is itself a list

Be careful with extend on strings: it splits the string into characters. If you want to add a string as a single element, use append. If you want to add multiple strings as separate elements, use extend with a list of strings:

words = ['hello', 'world'] list_of_words = [] list_of_words.extend(words) print(list_of_words) # ['hello', 'world']

Compatibility and Edge Cases

Both methods are available in all supported Python versions (3.x). They behave consistently across CPython, PyPy, and other implementations, though performance characteristics may vary slightly.

One edge case: extend with a dictionary iterates over its keys, not its key-value pairs. If you need to add dictionary items as tuples, pass dict.items() explicitly.

d = {'a': 1, 'b': 2} list_of_keys = [] list_of_keys.extend(d) print(list_of_keys) # ['a', 'b'] list_of_pairs = [] list_of_pairs.extend(d.items()) print(list_of_pairs) # [('a', 1), ('b', 2)]

Another edge case is using extend with a tuple or a range. Both are iterables, so the elements are added individually. append with a tuple adds the tuple as a single element.

Choosing the Right Method for Maintainable Code

Beyond correctness, the choice between append and extend affects code readability. append clearly signals that one item is being added. extend signals that an iterable's contents are being merged. Using the wrong method can confuse readers and introduce subtle bugs.

When refactoring, prefer extend over a loop of append calls when you have an existing iterable. This reduces boilerplate and makes the intention explicit. However, if the iterable is generated lazily and you need to process each element before adding it, a loop with append may be clearer.

# Clearer with append when processing each item processed = [] for item in raw_data: processed.append(transform(item)) # Clearer with extend when merging already transformed iterables processed = [] processed.extend(transform_all(raw_data))

In summary, append and extend serve distinct purposes. append adds a single object to a list; extend adds each element of an iterable. Understanding this difference prevents common mistakes and helps you write more efficient, readable Python code.

python append vs extend: Practical Usage and Code Examples | RYUSLOG DEV