*middle, = t
Built-in Data Structures

Meaning

Unpacks an iterable into a list assigned to the variable middle, capturing all elements.

Primary Function

Unpack iterable into a list variable using extended unpacking syntax.

Communicative Purpose

Express the intent to collect all elements of an iterable into a list variable for later use.

Pattern

*middle, = t

Core Structure

*<target>, = <iterable>

Função primária

Unpack iterable into a list variable using extended unpacking syntax.

Propósito comunicativo

Express the intent to collect all elements of an iterable into a list variable for later use.

Situações de gatilho

When you need to collect all items from an iterable (list, tuple, generator, etc.) into a list without needing to separate head or tail elements.

Contextos

Used in assignment statements, often in loops, function parameters, or when processing sequences where you want to retain all items as a list.

Padrão

*middle, = t

Estrutura central

*<target>, = <iterable>

Slots de substituição

{"middle": "variable name", "t": "iterable expression"}

Colocados típicos

  • ["list"
  • "tuple"
  • "generator"
  • "range"
  • "zip"
  • "enumerate\[\]

Substituições comuns

  • ["*rest
  • = seq"
  • \

Erros comuns

Using a trailing comma without the star, e.g., "middle, = t", which raises a SyntaxError Placing the star on the right side of the assignment, e.g., "t = *middle,", which is invalid syntax Assuming the unpacked result is a tuple; it is actually a list, leading to unexpected list methods

Similar / contraste

Standard unpacking without star (a, b = seq) – only works for fixed length Using slicing (seq[1:-1]) – creates a new list but lacks the syntactic clarity of extended unpacking

Interferências

Coming from JavaScript: using spread syntax "..." in assignment – Python uses "*" for extended unpacking

Família do chunk

  • list unpacking
  • tuple unpacking
  • extended iterable unpacking

Nuance

Do not use when the iterable may be extremely large, as it materializes the whole sequence in memory; the operation is O(n) time and memory; it fails if the iterable is not iterable (e.g., None)

Efeito pragmático

Enables concise collection of all elements while keeping the assignment syntax tidy, reducing boilerplate loops

Dica de memória

Think of the star as a net that catches every remaining fish in the sequence after the first cast

Nota

Available since Python 3.0; in Python 2 the syntax required a trailing comma after the star target

Upgrade path

head, *middle, tail = seq # capture head, middle, and tail

Frequência: MediumFormulaicidade: Semi-fixedTipo de construção: extended unpacking patternPrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Immediate

Log in to save chunks.