Creating and Unpacking Tuples
Tuples are immutable collections created using comma-separated values, optionally enclosed in parentheses. Once created, their elements cannot be modified.
A Comma Can Change the Meaning
Tuple syntax has one especially important detail: for a single-item tuple, the comma matters more than the parentheses. The expressions (5) and (5,) may look almost identical, but they do not represent the same kind of value. The first is a parenthesized expression containing the number 5. The second is a tuple containing one item. Learning to see that difference prevents mistakes when code expects a tuple, returns tuple results, or accesses values by index.
What do you think happens?
Which expression creates a single-item tuple?
Reveal answer
Answer: (5,)
The comma tells Python that the value is part of a tuple. Parentheses by themselves can group an expression, so (5) is treated as the number 5 in parentheses rather than as a tuple.
The Comma Makes the Tuple
Tuples are immutable collections. They are created from comma-separated values, and parentheses are optional in many tuple expressions. For example, (1, 2, 3) and 1, 2, 3 use comma-separated values to form tuples. Once a tuple has been created, its elements cannot be modified.
A single-item tuple is a tuple containing exactly one value, written with a trailing comma: (item,). The comma distinguishes the tuple from a parenthesized expression.
Empty and Single-Item Forms
Three tuple sizes
Identify the structure represented by each expression.
(): Empty parentheses create an empty tuple. It contains no items.
(2,): The trailing comma creates a tuple containing one item: the number 2.
(1, 2, 3): The comma-separated values create a tuple containing three items. The parentheses are optional for this comma-separated form.
The empty form is (), the single-item form is (2,), and the multi-item form is (1, 2, 3).
An empty tuple is written as (). Unlike the single-item case, it has no ambiguity: empty parentheses create an empty tuple. An empty tuple can be useful when a variable must begin as a tuple before it receives values, or when a function expects a tuple argument but there are no items to pass.
no_matches = () one_match = (2,) many_matches = 1, 2, 3
Reading Nested Tuple Values
Tuple elements are accessed by index just like list elements. When a tuple contains another tuple, use one index to select the inner tuple and another index to select an item inside it. Each index answers a separate question: which item at the current level should Python select?
In coordinates[0][0], the first [0] selects the first inner tuple, (10, 20). The second [0] selects the first value inside that inner tuple, 10. Nested indexing therefore moves through the structure one level at a time.
Unpacking Values
Unpacking assigns the values in a tuple to separate variables. The tuple provides the grouped values, while the variables provide individual names for those values. This is useful when a tuple represents several related results that should be handled separately.
The first variable receives the first tuple value, and the second variable receives the second tuple value. The tuple remains a grouped collection, but unpacking gives each value a separate variable name for later use.
Protection from Accidental Changes
Tuples are immutable, which means their elements cannot be modified after the tuple is created. Lists, by contrast, allow their elements to be modified. Choose a tuple when you need to guarantee that grouped data will not change, such as function return values or dictionary keys. Choose a list when the collection needs full modification.
settings = ("safe", "fixed") items = ["changeable", "collection"] items[0] = "updated" settings[0] = "updated"
Mistakes with Tuple Syntax
Omitting the trailing comma in a single-item tuple
Parentheses alone group the number 5 as an expression. They do not create a tuple.
Fix:
Write value = (5,) so the comma identifies the expression as a single-item tuple.Assuming every parenthesized expression is a tuple
Parentheses have more than one use in Python, including grouping expressions. The comma, not the parentheses alone, creates the tuple.
Fix:
Use result = (item,) when the intended value is a tuple containing one item.Trying to modify a tuple as though it were a list
Tuple elements cannot be modified after the tuple is created.
Fix:
Use a list when the collection needs full modification, or create a different tuple with the desired values.Treating a nested tuple as a flat collection
The outer tuple contains inner tuples. Reaching a value inside an inner tuple requires another index.
Fix:
Use one index for each level, such as coordinates[0][0] for the first value in the first inner tuple.
Practice the Distinctions
For each expression, identify whether it is an empty tuple, a single-item tuple, a multi-item tuple, or a parenthesized expression. Then identify the value selected by nested indexing.
Hints
- Look for a comma in every single-item tuple.
- For an empty tuple, check whether the parentheses contain anything.
- For nested indexing, evaluate the first index before the second.
What do you think happens?
What does nested[1][0] select?
Reveal answer
Answer: third
The first index, 1, selects the second inner tuple, ("third", "fourth"). The second index, 0, selects the first value in that inner tuple: "third".
Key Takeaways
- Use () to create an empty tuple.
- Use (item,) to create a single-item tuple; the trailing comma is essential.
- Tuples are created from comma-separated values, with optional parentheses in multi-item forms.
- Tuple elements can be accessed by index, including through multiple levels of nested tuples.
- Tuples are immutable, so use them for grouped data that must not change; use lists when elements need modification.
Key Takeaways
- The comma, rather than parentheses alone, creates a single-item tuple.
- Empty tuples use (), while single-item tuples use a trailing comma such as (item,).
- Multi-item tuples use comma-separated values, and parentheses are optional in many cases.
- Nested tuple values are reached by applying indexes one level at a time.
- Tuples protect their elements from modification, unlike lists.