Meaning
Creates a mutable ordered collection of items by assigning a list literal to a variable. This avoids the need to build a list incrementally with multiple append calls, providing immediate readability and performance. Use it when you have a known set of values to store or pass around.
Primary Function
Data structure initialization
Communicative Purpose
Define a sequence of values for later use in computation.
Pattern
variable = [item1, item2, item3]
Core Structure
... = [...]
Função primária
Data structure initialization
Propósito comunicativo
Define a sequence of values for later use in computation.
Situações de gatilho
Python scripting: when you need a fixed set of items to iterate over; Python scripting: when you need to store multiple related values; Python scripting: when passing a collection of arguments to a function
Contextos
General Python scripts, data‑processing pipelines, educational examples, quick prototyping.
Padrão
variable = [item1, item2, item3]
Estrutura central
... = [...]
Slots de substituição
variable name: identifier, elements: comma‑separated expressions
Colocados típicos
- for loops
- list comprehensions
- function arguments
- slicing operations
Substituições comuns
- Using tuple literals '(...)' for immutable sequences
- using list() constructor for dynamic creation.
Erros comuns
Omitting commas between elements: causes a syntax error (invalid syntax) leading to runtime failure when the code is parsed.; Using a mutable list as a default function argument: causes the same list object to be reused across calls, leading to unexpected state sharing and bugs.; Mixing tabs and spaces inside the literal: causes IndentationError if the literal is indented incorrectly, leading to runtime failure.
Similar / contraste
Tuple literal '(1, 2, 3)' creates an immutable sequence; array.array provides typed, compact storage.
Interferências
Coming from JavaScript: assuming array literals are immutable → Python lists are mutable and support heterogeneous types.
Família do chunk
- sequence literals
- collection literals
- mutable containers
Nuance
When NOT to use this: avoid for large, fixed datasets where a tuple or array would be more memory‑efficient. Performance implications: lists have higher memory overhead per element than tuples or array.array due to mutability and pointer overhead. Non-obvious boundary conditions: list literals create a new object each time; using them in a loop can cause unintended object churn if not intended.
Efeito pragmático
Enables indexed access, mutability, and easy iteration over a collection of items.
Dica de memória
Think of a list literal as a pre‑packed lunchbox: you lay out your items in order, close the lid, and it’s ready to be carried anywhere in your code.
Nota
Each list literal creates a distinct list object; avoid using mutable lists as default function arguments.
Upgrade path
list comprehension: [expr for item in iterable if condition] or generator expression for lazy evaluation.
Log in to save chunks.