a, *b, c = t
Built-in Data Structures

Meaning

The statement `a, *b, c = t` unpacks a sequence so that the first element is assigned to `a`, all middle elements are collected into a list `b`, and the last element goes to `c`. It solves the pain of manually slicing a sequence to obtain head, tail, and interior, which can be verbose and error‑prone. Use it whenever you have an iterable of unknown length but need explicit access to its first and last items together with the remaining items.

Primary Function

Sequence unpacking

Communicative Purpose

Extracts the first, middle, and last elements from an iterable in one statement.

Pattern

first, *middle, last = seq

Core Structure

... , * ... , ... = ...

Função primária

Sequence unpacking

Propósito comunicativo

Extracts the first, middle, and last elements from an iterable in one statement.

Situações de gatilho

Data processing: extracting the first, middle, and last items of a list; CSV parsing: separating the first column, the remaining columns, and the last column; Function return handling: capturing a variable‑length result while keeping the initial and final values.

Contextos

Python codebases, data processing scripts, algorithmic challenges, any place working with sequences.

Padrão

first, *middle, last = seq

Estrutura central

... , * ... , ... = ...

Slots de substituição

first_var: identifier, middle_vars: identifier for collected rest, last_var: identifier, seq: iterable

Colocados típicos

  • tuple unpacking
  • extended iterable unpacking
  • functions returning (head
  • *middle
  • tail)

Substituições comuns

  • first = seq[0]
  • middle = seq[1:-1]
  • last = seq[-1]

Erros comuns

Assuming *b yields a tuple; forgetting that *b creates a list; misplacing the star causing ValueError when the iterable is too short.

Similar / contraste

Simple unpacking a, b, c = t (requires exactly three items); head, *tail = t (splits first and rest); *head, tail = t (splits rest and last).

Interferências

Coming from C: expecting pointer‑like dereference → in Python the asterisk is used for iterable unpacking, not for memory address access.

Família do chunk

  • extended iterable unpacking
  • sequence unpacking
  • destructuring

Nuance

If the iterable has exactly two elements, *b becomes an empty list; with one element the statement raises ValueError. Creates a new list for *b, which may have performance implications for very large iterables.

Efeito pragmático

Makes intent explicit to capture head, middle, and tail in one readable line.

Dica de memória

Think of grabbing the first, the rest, and the last from a line.

Nota

If the iterable has exactly two elements, *b becomes an empty list; with one element the statement raises ValueError. Creates a new list for *b, which may have performance implications for very large iterables.

Upgrade path

head, *middle, tail = seq # same as pattern; alternatively use slicing: head = seq[0]; middle = seq[1:-1]; tail = seq[-1]

Frequência: MediumFormulaicidade: Semi-fixedTipo de construção: extended iterable unpacking (head, *middle, tail)Prioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Medium-termIdioma?: Sim

Log in to save chunks.