Lists, Tuples, and Sequence Design
Store ordered data with lists and tuples, slice sequences safely, unpack values, and choose mutability based on the meaning of the data.
Before this lesson
Choose list or tuple by semantics
Use slicing and unpacking clearly
Avoid aliasing and mutation traps
The short answer
Use a list for an ordered collection that changes and a tuple for a fixed-position record or immutable grouping. Iterate directly, copy deliberately, and avoid sharing mutable lists accidentally.
Build the mental model
Lists and tuples both implement sequence operations such as indexing, slicing, membership, and iteration. Lists expose mutating methods; tuples do not. Tuple immutability is shallow—a contained list can still change.
The central idea in this lesson is sequence semantics. Read the rule, predict a concrete outcome, and then run the smallest example that can confirm or reject the prediction. This separates knowledge from familiarity with syntax.
Make the design choice explicit
Use append to add one item and extend to add items from another iterable. Slices create new sequences. Unpacking makes fixed shapes visible, while starred unpacking captures a variable middle section.
readings = [17, 21, 19, 24]
readings.append(22)
first, *middle, last = readings
print(f"First: {first}, last: {last}")
print(f"Middle readings: {middle}")
print(f"Highest: {max(readings)}")Expected output
First: 17, last: 22 Middle readings: [21, 19, 24] Highest: 24
Trace the example before running it. Identify each input, transformation, returned value, and side effect. Then change one boundary value and explain why the new behavior follows from the rule rather than memorizing the output.
Recognize failure modes
Assignment does not copy a list; two names can reference the same object. Use list(existing) or a slice for a shallow copy, and understand that nested mutable values still remain shared.
Use a professional practice loop
Turn the concept into a repeatable workflow: write down the expected behavior, implement one coherent change, run a representative example, and retain a regression check. If the result surprises you, capture the exact input and error before changing anything.
Review the program for names, boundaries, and hidden side effects. A solution is complete when another developer can understand its contract, reproduce its setup, and verify both the successful path and one meaningful failure path.
Quick knowledge check
Answer before you reveal.
01Does assigning second = first copy a list?
No. Both names refer to the same list until you explicitly create a copy.
02What four steps make the practice loop reliable?
Predict the behavior, make one coherent change, run it, and retain a check that detects regression.
Exercise
Practice challenge
Maintain an ordered reading list, add and remove values without mutating the original input, and return minimum, maximum, and median information.
Requirements
- The successful path produces the documented result
- At least one boundary or invalid case is handled deliberately
- Calculation or domain logic is separated from console interaction
Optional extension: Add one automated regression check for the most important rule.
Open in Python compilerLesson checkpoint
One small step locks it in
Mark this lesson complete, then keep the momentum going.
Clear up the details
Frequently asked questions
Is sequence semantics only important in large programs?
No. Small programs reveal the same rules with less noise, and learning the rule early prevents fragile habits from becoming architecture.
Should I memorize every API used here?
No. Memorize the mental model and how to verify behavior. Use documentation for exact names and parameters when needed.
How do I know the exercise is finished?
Meet every success criterion, test at least one boundary or failure case, and explain why the output follows from the code.