Code Reference

Tuples

Python · Reference cheat sheet

Tuples

Python · Reference cheat sheet


📋 Overview

A tuple is an ordered, immutable sequence. Use for fixed records, multiple return values, and hashable keys (when all elements are hashable). Prefer tuples over lists when the length and contents should not change.

🔧 Core concepts

OperationExample
Create(1, 2), 1, 2, tuple(iterable), ()
Single element(1,) — trailing comma required
Index / slicet[0], t[1:]
Unpacka, b = t, a, *rest = t
Concat / repeatt + u, t * n (new tuples)
Namedcollections.namedtuple, typing.NamedTuple

Immutability means you cannot assign t[i] = x or append. Nested mutable objects inside a tuple can still mutate.

💡 Examples

Returns and unpacking:

def divmod_pair(a: int, b: int) -> tuple[int, int]:
    return a // b, a % b

q, r = divmod_pair(17, 5)

As dict keys:

point = (3, 4)
grid: dict[tuple[int, int], str] = {point: "occupied"}
print(grid[(3, 4)])

NamedTuple (typed record):

from typing import NamedTuple

class User(NamedTuple):
    id: int
    name: str
    active: bool = True

u = User(1, "Ada")
print(u.name, u._asdict())

Tuple vs list choice:

coords = (10.0, 20.0)      # fixed pair — tuple
history: list[float] = []  # grows over time — list
history.append(1.5)

⚠️ Pitfalls

  • (1) is just int 1 — use (1,) for a one-element tuple.
  • Tuples containing lists/dicts are not hashable.
  • t += (x,) rebinds t to a new tuple (ok for local names; confusing for aliases).
  • Prefer NamedTuple / dataclass(frozen=True) over anonymous long tuples for clarity.
  • Slicing a tuple returns a tuple, not a list.

On this page