Tuple Unpacking and Multiple Assignment
Tuples support read-only indexing and slicing operations identical to lists.
From Positions to Names
A tuple stores values in positions. Tuple unpacking and multiple assignment use those positioned values as inputs for separate variable names. The key idea is to read the tuple rather than change it. Tuples support read-only indexing and slicing, so the positions can be inspected, but the existing tuple cannot be edited in place.
Reading Tuple Positions
A bracket expression that selects one tuple position is indexing. Indexing returns a single element at that position. The returned object has whatever type that element has. A slice selects a range of positions instead. Slicing returns a new tuple containing the selected range. Both operations are read-only and succeed on valid indices.
Selecting a Value or a Range
Given t = ('a', 'b', 'c', 'd', 'e'), compare t[0] with t[0:1].
Use indexing: t[0] selects the first position and returns a single element.
Use slicing: t[0:1] selects a range containing the first position and returns a tuple.
Use the distinction: The two expressions are not interchangeable because one produces an element and the other produces a tuple.
Indexing gives one element; slicing gives a tuple containing the selected range.
Why In-Place Changes Fail
Tuples are immutable. You can read any valid position, but you cannot assign a new value to an existing position. An attempted assignment such as t[0] = 'new_value' raises a TypeError. This differs from the behavior learners may expect after working with lists, where an element assignment can be used to modify a list.
Building a Replacement Tuple
To achieve a changed state, create a new tuple and rebind the variable to that new tuple. The common pattern is to keep part of the original tuple with slicing, create a one-element tuple for the replacement value, concatenate the pieces, and assign the result back to the variable.
Changing the First Value Safely
Create a tuple whose first value is 'A' while preserving the remaining values from t = ('a', 'b', 'c', 'd', 'e').
Keep the remaining range: The slice t[1:] extracts ('b', 'c', 'd', 'e').
Make a one-element tuple: ('A',) is a tuple containing the replacement value. The comma distinguishes this tuple from a parenthesized single value.
Concatenate: ('A',) + t[1:] combines the replacement tuple with the remaining slice.
Rebind: Assign the resulting fresh tuple back to t. The original tuple is not edited in place.
t refers to ('A', 'b', 'c', 'd', 'e').
Mistakes to Avoid
Treating a tuple like a list during element assignment.
Tuple elements cannot be modified in place, so the assignment raises a TypeError.
Fix:
Construct a new tuple with slicing and concatenation, then rebind the variable.Confusing an indexed element with a sliced tuple.
Indexing returns one element, while slicing returns a tuple.
Fix:
Use a slice when you need a tuple range, or wrap a replacement value as a one-element tuple.Concatenating a string directly with a tuple.
The operands have different types: the left side is a string and the slice is a tuple.
Fix:
Use ('A',) + t[1:] so both operands are tuples.Assuming rebinding modifies the old tuple.
The operation creates a fresh tuple; it does not alter the original tuple in place.
Fix:
Think of the variable as being rebound to a new tuple.
Practice the Transformation
Suppose t is the tuple ('a', 'b', 'c', 'd', 'e'). Describe the result of t[1:], explain why t[0] and t[0:1] are different kinds of results, and give the concatenation pattern that creates a new tuple with 'A' in the first position.
Hints
- A slice beginning at index 1 keeps the values after the first position.
- Indexing returns one element; slicing returns a tuple.
- Use a one-element tuple for the replacement before concatenating.
What do you think happens?
What happens when a program evaluates ('A' + t[1:]) for a tuple t?
Reveal answer
Answer: The expression raises a TypeError.
The expression attempts to concatenate a string with a tuple. The replacement must be written as a one-element tuple: ('A',) + t[1:].
Stable Data by Design
Tuple immutability is useful when data should remain stable. The source material identifies tuples as safe to use as dictionary keys, whereas lists cannot be keys because they are mutable. It also describes tuples as useful in function signatures and data structures that need to remain stable, because a function receiving a tuple cannot accidentally modify that tuple.
- Tuple unpacking and multiple assignment work with values held at tuple positions, while tuple access remains read-only. Indexing returns one element. Slicing returns a tuple. Attempting to assign through a tuple index raises a TypeError. To change the contents, build a new tuple with slicing and concatenation, then rebind the variable.
Key Takeaways
- Tuple indexing reads one element, while tuple slicing reads a range and returns a tuple.
- Both indexing and slicing are read-only operations on tuples.
- Tuple elements cannot be modified in place; element assignment raises a TypeError.
- To create changed contents, concatenate a replacement tuple with slices of the original and rebind the variable.
- A one-element tuple such as ('A',) is different from the single element 'A' when concatenation is required.