[x * 2 for x in numbers]
Performance Patterns

Meaning

Creates a new list by multiplying each element in the input iterable 'numbers' by 2.

Primary Function

Produces a new list where each element is doubled.

Communicative Purpose

Expresses a concise transformation of a collection via list comprehension.

Pattern

[x * 2 for x in iterable]

Core Structure

[expression for item in iterable]

Função primária

Produces a new list where each element is doubled.

Propósito comunicativo

Expresses a concise transformation of a collection via list comprehension.

Situações de gatilho

When you need to transform each element of a collection by applying a uniform operation (e.g., scaling).

Contextos

Used in data processing loops, numerical transformations, functional-style Python code.

Padrão

[x * 2 for x in iterable]

Estrutura central

[expression for item in iterable]

Slots de substituição

x: variable representing each element; numbers: iterable of numbers; 2: multiplier expression (can be any expression).

Colocados típicos

  • numbers
  • data
  • items
  • values

Substituições comuns

  • x*2 can be replaced with any expression like x+1
  • x*len
  • func(x).

Erros comuns

Using assignment instead of expression, forgetting brackets, using wrong variable name, unintentionally modifying original list.

Similar / contraste

map(lambda x: x*2, numbers), [x for x in numbers], [x*2 for x in numbers if x>0]

Interferências

Confusing with generator expression (x*2 for x in numbers) which yields a generator, not a list.

Família do chunk

  • list comprehension
  • map
  • filter
  • generator expression

Nuance

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

Efeito pragmático

Conveys intent to transform data succinctly, idiomatic Python.

Dica de memória

double each number

Tipo de construção: list comprehensionTag de espaçamento: Short-termIdioma?: Sim

Log in to save chunks.