[x*2 for x in range(5)]
Iteration Patterns

Meaning

Creates a list containing each number from 0 to 4 multiplied by 2.

Primary Function

Generate a transformed list via list comprehension.

Communicative Purpose

Express a concise transformation of a range into a list.

Pattern

[expression for item in iterable]

Core Structure

[expression for item in iterable]

Função primária

Generate a transformed list via list comprehension.

Propósito comunicativo

Express a concise transformation of a range into a list.

Situações de gatilho

When you need to produce a list of transformed values from a range or iterable.

Contextos

Python list comprehensions, data transformation, functional‑style programming.

Padrão

[expression for item in iterable]

Estrutura central

[expression for item in iterable]

Slots de substituição

{"expression":"x*2","item":"x","iterable":"range(5)"}

Colocados típicos

  • range list map filter lambda

Substituições comuns

  • {"expression":"any expression involving the item variable"
  • "item":"any valid variable name"
  • "iterable":"any iterable object"}

Erros comuns

forgetting the square brackets (creating a generator expression) misspelling the iteration variable using parentheses instead of brackets using an invalid iterable

Similar / contraste

map(lambda x: x*2, range(5)) [x*2 for x in range(5) if x%2==0] [x*2 for x in range(5)] == list(map(lambda x: x*2, range()))]

Interferências

Confusing with generator expression (x*2 for x in range(5)) which yields a generator object Confusing with map() which returns an iterator in Python 3

Família do chunk

  • list comprehension patterns

Nuance

Produces a new list; does not modify the original iterable; evaluation is eager.

Efeito pragmático

Conveys a concise, idiomatic way to apply a transformation to a sequence in Python.

Dica de memória

Think ‘double each number from zero to four’.

Nota

Common idiom for simple transformations; often preferred over map for readability.

Upgrade path

Consider generator expressions for memory efficiency or map/filter for functional style.

Frequência: Very highFormulaicidade: Semi-fixedTipo de construção: list comprehensionPrioridade de aquisição: Automatic productionPrioridade de output: BothTag de espaçamento: Immediate

Log in to save chunks.