Indexing and Slicing Collections
Tuples are immutable collections created using comma-separated values, optionally enclosed in parentheses. Once created, their elements cannot be modified.
Try it: Indexing and Slicing
How Python indexes a sequence from 0 (or from the end with negative numbers) and how a slice start:stop:step picks positions — with the exact CPython rules for omitted and out-of-range bounds.
How it works
- Positions run 0 … len−1; negative positions count back from the end (−1 is the last).
- An index outside the string raises IndexError.
- A slice resolves start and stop (omitted → the ends; out of range → clamped), then walks by step, stopping before stop.
- A negative step walks backwards; a step of 0 is a ValueError.
Default run (7 steps): 'PYTHONIC'[1:6:2]: a slice copies characters from start up to (not including) stop, moving by step. … 'PYTHONIC'[1:6:2] → 'YHN' (positions 1, 3, 5).
Simplified: Works on a short string (up to 16 printable ASCII characters); lists slice by exactly the same rules.
Loading the simulation…
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.
- 1Start with the tuple
The tuple contains three ordered values: red, green, and blue.
- 2Use index 1
Indexing selects the value stored at position 1.
- 3Read 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.
- 1Select the outer item
Index 1 selects the inner tuple ("Lin", 29).
- 2Select the inner item
Index 0 of the selected inner tuple is Lin.
- 3Combine 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
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 |
The source-supported distinction between tuples and lists
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
Common mistake Assuming parentheses alone are the important part of tuple creation.
What it looks likecoordinates = 4, 9Why it failsThe source describes tuples as comma-separated values with optional parentheses.
Do this insteadFocus on the comma-separated-value pattern; parentheses may be included but are optional in the stated tuple syntax.
Common mistake Treating a tuple like a list that can be updated.
What it looks likefixed_values = (10, 20), followed by an attempt to replace one elementWhy it failsTuple elements cannot be modified after the tuple is created.
Do this insteadUse a list when the elements need full modification, or use a tuple when the values should remain fixed.
Common mistake Stopping after the outer index when working with nested tuples.
What it looks likeSelecting an inner tuple but not selecting the value inside it
Why it failsA nested tuple is itself a tuple, so reaching an inner value requires another index.
Do this insteadTrace the access in stages: first select the inner tuple from the outer tuple, then select an element from that inner tuple.
Common mistake Assuming indexing changes the collection.
What it looks likeReading one tuple element and expecting the tuple to be rewritten
Why it failsIndexing accesses an element; immutability concerns attempts to modify an element.
Do this insteadSeparate 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.