Lists & Collections — Lesson 2: Update a copy, not the original
New skill: List copying and assignment
Previously learned: Units 01–07
Why this matters
A class app might preview edited settings without overwriting the original choices.
Step 1 — Explore working Python
Read the program below. Before running it, write down the result you expect.
original = ['red', 'blue']
updated = original.copy()
updated[-1] = 'green'
print(original)
print(updated)
Step 2 — Trace the code
- Create a new list with
.copy(). - Assign a new value at an existing index.
- Confirm that the original list still has its previous value.
Step 3 — Check your understanding
If a = [1, 2] and b = a, what changes when b[0] = 9? Compare with .copy().
Step 4 — Watch for this mistake
copy = items gives two names to the same list. It does not make an independent copy.
Practice: Update a copy, not the original
Complete replace_last(items, value) to return a new list with its final item replaced. Do not modify items. The list is nonempty.
Function contract: replace_last(items, value)
Starter file: main.py
Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.
Hint 1 — Start
Use `items.copy()` first.Hint 2 — Build
Update position -1 in the copied list.Hint 3 — Test
Return the copy.Reflect
In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.
Optional reading
For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.