a, b = t
Built-in Data Structures

Meaning

Assigns elements of an iterable to multiple variables in a single statement, unpacking the iterable's items. This enables concise extraction of values from sequences such as tuples, lists, or ranges.

Primary Function

Data unpacking

Communicative Purpose

Extract values from a sequence into separate named variables for clearer code.

Pattern

first_var, second_var = iterable

Core Structure

... , ... = ...

Função primária

Data unpacking

Propósito comunicativo

Extract values from a sequence into separate named variables for clearer code.

Situações de gatilho

Python: swapping two values without a temporary variable; Python: splitting a pair or tuple into individual variables; Python: iterating over pairs in a loop

Contextos

Common in Python scripts, data processing pipelines, loops over enumerated items, and functions returning multiple values.

Padrão

first_var, second_var = iterable

Estrutura central

... , ... = ...

Slots de substituição

first_var: identifier, second_var: identifier, iterable: expression yielding at least two items

Colocados típicos

  • swapping values
  • loop iteration
  • function returns with multiple values

Substituições comuns

  • Extended unpacking with star
  • e.g.
  • a
  • *rest = t
  • using indexing like a = t[0]
  • b = t[1]

Erros comuns

Mismatched number of variables and iterable length causing ValueError; assuming unpacking works with non-iterable objects; forgetting parentheses when needed.

Similar / contraste

Sequential assignment (a = t[0]; b = t[1]) – more explicit but verbose; attribute unpacking like obj.x, obj.y = t – assigns to attributes instead of variables.

Interferências

Coming from Java: may try to assign via temporary variable → use tuple unpacking a, b = t

Família do chunk

  • tuple unpacking
  • sequence unpacking
  • parallel assignment

Nuance

Do not use when the iterable length may not match the number of variables unless using star unpacking, as it raises ValueError; performance impact is minimal—just O(n) iteration with no extra copying; note that the right side is evaluated first, enabling swaps, and generators are exhausted after unpacking.

Efeito pragmático

Makes code more readable and avoids indexing errors; enables idiomatic swaps and clear multiple return value handling.

Dica de memória

Think of unpacking a suitcase: pull out each item into its own slot.

Nota

Use parentheses when unpacking in return statements or when ambiguity may arise; otherwise parentheses are optional.

Upgrade path

a, *rest = t (extended unpacking)

Frequência: HighFormulaicidade: Semi-fixedTipo de construção: Multiple assignment (parallel assignment) used for unpacking iterables into variables.Prioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Medium-term

Log in to save chunks.