add
list.extend
Extends the list by appending every element from an iterable, in order. Mutates in place and returns None; equivalent to `list += iterable`.
list.extend(iterable) Parameters
| Parameter | Purpose |
|---|---|
| iterable | Any iterable (list, tuple, set, str, generator, range) whose elements are appended one by one. |
| str argument | A string is iterable — extend('ab') adds 'a' then 'b', not the whole string. Often a bug source. |
Examples
>>> lst = [1, 2]
>>> lst.extend([3, 4])
>>> lst
[1, 2, 3, 4] Adds each element of the iterable individually.
>>> lst = [1, 2]
>>> lst.extend('ab')
>>> lst
[1, 2, 'a', 'b'] Strings iterate as characters — a common gotcha.
>>> lst = []
>>> lst.extend(range(3))
>>> lst
[0, 1, 2] Any iterable works — including generators and range.
>>> lst = [1]
>>> lst.extend(5)
TypeError: 'int' object is not iterable Non-iterable args raise TypeError — use append for a single value.
Gotcha
extend(non_iterable) raises TypeError; extend('abc') iterates the string into characters. Returns None — do not chain or assign. `a += b` is the operator form.
Related methods
list.append
Appends a single item x to the end of the list, mutating it in place. Always returns None — it does not return the modified list.
list.insert
Inserts item x before index i, shifting subsequent elements right. Mutates in place, returns None; O(n) because it shifts trailing elements.
list.copy
Returns a shallow copy of the list — a new list containing the same element references. Equivalent to lst[:] or list(lst); does not recursively copy nested mutables.
← All Python list methods · Dict methods · list vs tuple vs set