Meaning
Extends a list by appending each element from another iterable. The original list is mutated in‑place and the method returns None. Use it when you need to add several items without creating a new list.
Primary Function
List manipulation
Communicative Purpose
Add multiple items to an existing list in a single operation.
Pattern
list.extend(iterable)
Core Structure
list.extend(...)
Função primária
List manipulation
Propósito comunicativo
Add multiple items to an existing list in a single operation.
Situações de gatilho
Data processing: concatenating a list with another sequence; Web scraping: accumulating URLs into a list
Contextos
Common in Python scripts, data‑processing pipelines, and any code that builds collections dynamically.
Padrão
list.extend(iterable)
Estrutura central
list.extend(...)
Slots de substituição
iterable: any iterable (list, tuple, generator, etc.)
Colocados típicos
- list
- extend
- iterable
- for loop
- += operator (though not equivalent)
Substituições comuns
- my_list += iterable
- my_list = my_list + list(iterable)
- for x in iterable: my_list.append(x)
Erros comuns
Passing a non‑iterable (e.g., a single int) causing TypeError; expecting extend to add the iterable as a single nested element rather than its items.
Similar / contraste
list.append adds the whole object as one element, while list.extend adds each element of the iterable individually.
Interferências
Coming from JavaScript's array.push, which can take multiple arguments; in Python push is append, not extend.
Família do chunk
- list.append
- list.insert
- list.remove
- list.pop
Nuance
extend works in‑place and consumes generators; using the same list as the source can produce unexpected results.
Efeito pragmático
Using extend efficiently adds multiple items to a list in-place, avoiding unnecessary copies and improving performance.
Dica de memória
Think of a conveyor belt that adds boxes to the end of a line without stopping the belt.
Nota
Note: extend returns None, so avoid using it in expressions where a list is expected (e.g., do not assign the result to a variable).
Upgrade path
Consider using itertools.chain for combining multiple iterables without building intermediate lists.
Log in to save chunks.