liveBook · Manning

Tuple

As explained in Python How-To

A tuple is an ordered, immutable collection of values that can represent a fixed set of fields.

Need to store a task’s creation time and user without changing either field? Use a tuple: an ordered container whose items can be read by index but cannot be replaced after creation. A list is the better choice for a collection of tasks that you expect to add to or remove from. section 3.1 section 3.1.1

Store fixed fields

metadata = ('2024-06-01', 'python_user') print(metadata[1]) # python_user metadata[1] = 'new_user' # TypeError: 'tuple' object does not support item assignment Use a tuple when each position has a distinct meaning and the fields should stay fixed. Use a list for items of the same kind that may change, such as the tasks in a task manager. section 3.1.1 section 3.1.2

Immutability applies to the tuple’s items, not necessarily to objects held in those positions. In `numbers = ([1, 2], [1, 2])`, `numbers[0].append(3)` changes an inner list without replacing the tuple item. Choose a tuple to signal that its structure should remain stable, not to guarantee that every contained object stays unchanged. section 3.1.3

Unpack fields into readable names

task = (1001, 'Laundry', 5) task_id, task_title, task_urgency = task print(task_id, task_title, task_urgency) # 1001 Laundry 5 One-to-one unpacking assigns each item to a corresponding variable. Parentheses are optional when creating a tuple: `task = 1001, 'Laundry', 5` also works. A function that appears to return multiple values likewise returns one tuple, which callers can unpack. section 4.4.1 section 6.2.3

Other practical uses follow the same fixed-fields pattern:

  • Collect extracted records as tuples of ID, title, and description, then keep those records in a list. section 2.5.2
  • Unpack the key-value tuples produced by dictionary `items()` while iterating. section 5.3.7
  • Pass two-item tuples to `dict()` as key-value pairs; `zip()` can pair items from two iterables. section 5.1.3

A plain tuple is concise, but positions alone do not identify fields to a reader. If a lightweight record needs named attributes, consider a named tuple rather than relying on indexing or unpacking throughout the code. Choose the container for the job, not simply because either option works. section 3.3.1 section 3.7

How Python How-To covers it

How deep it goes
Explains immutability through assignment errors, distinguishes mutable contents, and demonstrates unpacking; stops short of tuple implementation internals.
Examples
Uses task metadata, an item-assignment error, three-field unpacking, regular-expression records, and key-value pairs.
What it assumes
Assumes familiarity with Python list operations and assignment while teaching tuple immutability and one-to-one unpacking through examples.
What you can do after
Build it from scratch as a fixed-field container, and unpack its items or choose a list when updates are required.
How it connects
Connects tuples to lists, sequence unpacking, function return values, named tuples, dictionary iteration, and iterable-based construction.

Also explained in

Compare how 15 books explain tuple →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.