Meaning
Accesses the first element of a sequence (list, tuple, string) by index 0. Used when you need the initial item in an ordered collection.
Primary Function
Element access
Communicative Purpose
Retrieve the first element from a sequence
Pattern
sequence[0]
Core Structure
...[0]
Função primária
Element access
Propósito comunicativo
Retrieve the first element from a sequence
Situações de gatilho
reading the first value from a list of sensor readings, getting the initial coordinate from a tuple representing a point, extracting the first character from a string for validation
Contextos
General Python code, data processing scripts, algorithm implementations, educational examples
Padrão
sequence[0]
Estrutura central
...[0]
Slots de substituição
sequence: any indexable type (list, tuple, str, etc.)
Colocados típicos
- len()
- slicing
- for loops
- indexing other positions
Substituições comuns
- t[-1] for last element
- t[1:] for rest of sequence
- next(iter(t)) for first element of an iterator
Erros comuns
IndexError when the sequence is empty, confusing t[0] with t[1] (second element), applying to non‑indexable types like set or dict without integer keys
Similar / contraste
t[-1] (last element), t[:1] (first element as a slice), next(iter(t), default) (first element via iterator with fallback). Distinction: t[0] raises IndexError on empty; slicing returns empty sequence; next with default avoids exception.
Interferências
Coming from languages where indexing starts at 1 (e.g., MATLAB, R): may think t[0] is the second element. Coming from zero‑based languages with explicit bounds checks (e.g., Java): may forget to verify length before accessing.
Família do chunk
- Element access
- slicing
- indexing
- unpacking
Nuance
Only works on sequences supporting tgetitemt with integer indices; O(1) for built-in list/tuple, O(n) for some custom types; unsafe on empty sequences without a length check.
Efeito pragmático
Provides direct, constant‑time access to the leading element; makes intent explicit compared to unpacking or iteration.
Dica de memória
Think 'zero‑based first' – the very first slot is at index 0.
Nota
Equivalent to t.Tgetitemt(0); raises IndexError on empty sequences; use conditional expression or slicing for safe access.
Upgrade path
Use slicing t[:1] to obtain the first element as a sequence, or itertools.islice(t, 1) for generic iterators.
Log in to save chunks.