Indexing and Slicing Collections
Tuples are immutable collections created using comma-separated values, optionally enclosed in parentheses. Once created, their elements cannot be modified.
A Collection That Resists Change
A tuple is a collection whose elements cannot be modified after the tuple is created. This makes tuples useful when a group of values should remain fixed. Python also lets you access tuple elements by index, just as you access elements in a list.
The central distinction is simple: tuples protect their elements from modification, while lists allow their elements to be modified.
Reading Values by Position
Indexing means accessing an element by its position. Tuples support indexing in the same way as lists. In the generated example below, the value at index 0 is the first value, and the value at index 1 is the second value.
Following an index
Determine which value is selected from the tuple ("red", "green", "blue") at index 1.
Start with the tuple: The tuple contains three ordered values: red, green, and blue.
Use index 1: Indexing selects the value stored at position 1.
Read the selected value: The value associated with index 1 is green.
The access selects green.
Creating Tuple Values
Tuples are created with comma-separated values. Parentheses may enclose those values, but the defining pattern described in the source is the comma-separated values themselves.
Both generated examples use comma-separated values. The first leaves out parentheses; the second includes them. In either case, the values are presented as a tuple according to the creation rule supplied by the source.
Reaching Through Nested Tuples
A tuple can be nested inside another tuple. This creates a hierarchical structure: an outer index first selects one item from the outer tuple, and that selected item can itself be accessed by another index if it is an inner tuple.
Tracing two indexes
Find the value reached by selecting index 1 from the outer tuple and then index 0 from the selected inner tuple.
Select the outer item: Index 1 selects the inner tuple ("Lin", 29).
Select the inner item: Index 0 of the selected inner tuple is Lin.
Combine the access steps: The first index chooses the inner tuple; the second index chooses a value inside it.
The nested access reaches Lin.
Protecting Fixed Data
Tuple immutability means that once a tuple has been created, its elements cannot be modified. A list has the opposite behavior described in the source: its elements allow full modification. Choose a tuple when you need to guarantee that a group of values will not change, including as a function return value or a dictionary key.
What do you think happens?
A tuple contains (10, 20), and code attempts to replace the value at one index. What should you predict?
Reveal answer
Answer: The tuple remains protected because its elements cannot be modified.
Immutability prevents modification of tuple elements after creation. A list, in contrast, allows its elements to be modified.
In this generated example, the tuple element replacement is not allowed because tuple elements cannot be modified. The list element replacement is allowed because lists permit full modification. The important prediction is about whether the collection can change, not about changing the index itself.
Choosing Between Tuples and Lists
| Collection | Element access | Element modification | Useful when |
|---|---|---|---|
| Tuple | Access by index, like a list | Not allowed after creation | Data must be guaranteed not to change |
| List | Access by index | Full modification is allowed | Data needs to remain changeable |
Use a tuple when protecting the values is part of the design. The source specifically identifies function return values and dictionary keys as situations where a tuple can help guarantee that the data will not change.
Mistakes with Tuple Access
Assuming parentheses alone are the important part of tuple creation.
The source describes tuples as comma-separated values with optional parentheses.
Fix:
Focus on the comma-separated-value pattern; parentheses may be included but are optional in the stated tuple syntax.Treating a tuple like a list that can be updated.
Tuple elements cannot be modified after the tuple is created.
Fix:
Use a list when the elements need full modification, or use a tuple when the values should remain fixed.Stopping after the outer index when working with nested tuples.
A nested tuple is itself a tuple, so reaching an inner value requires another index.
Fix:
Trace the access in stages: first select the inner tuple from the outer tuple, then select an element from that inner tuple.Assuming indexing changes the collection.
Indexing accesses an element; immutability concerns attempts to modify an element.
Fix:
Separate read operations from modification attempts. Tuple elements can be accessed, but they cannot be modified.
Practice the Trace
For the nested tuple (("Ada", 36), ("Lin", 29)), trace the access that selects the second inner tuple and then selects its first value. State the value reached and explain why attempting to replace that value would not modify the tuple.
Hints
- Begin with the index in the outer tuple.
- The selected outer item is itself a tuple.
- Use a second index to select the value inside that inner tuple.
- Apply the immutability rule to the attempted replacement.
Decide whether each collection is a better fit for a tuple or a list: values that must not change, and values that need full modification. Explain your choice using immutability.
Hints
- A tuple protects its elements from modification.
- A list allows full modification.
- The source identifies function return values and dictionary keys as tuple use cases.
Key Takeaways
- Tuples are created from comma-separated values, with optional parentheses.
- Tuple elements can be accessed by index just like list elements.
- Nested tuples require tracing the outer index and then the inner index.
- Tuples are immutable, so their elements cannot be modified after creation.
- Use tuples for data that must remain fixed, and use lists when full modification is required.