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














