Lesson 14 of 26 · about 15 min
Mutability, aliasing and cloning
Two names can refer to the same list. Change it through one name and the other sees the change too. Copy the list when you want independence.
🧭 What you need first: Lists
You and your partner share one shopping list on the fridge. You call it “the list,” they call it “groceries.” Different names, same paper. When they cross out milk, it’s crossed out on your list too, because there is only one list.
That’s an alias: two names, one object.
If you each want your own list, you need to photocopy it. Now crossing something out on one doesn’t touch the other. That’s a clone.
Another way: a shared Google Doc
Sharing a link to a doc gives someone access to the same document; their edits appear for you. Making a copy (“File → Make a copy”) gives them their own document that changes independently.
Another way: a house with two keys
Two keys to one house. Whoever paints the kitchen, the kitchen is painted for both. Giving someone a key isn’t the same as building them a new house.
Spot the shared list
Watch the colored #1, #2 badges. Names with the same number point to the same list. Step through and see what happens to mine when yours changes.
Watch the code run, one step at a time
- 1mine = ["milk", "eggs"]
- 2yours = mine
- 3yours.append("bread")
- 4print(mine)
- 5copy = mine[:]
- 6copy.append("jam")
- 7print(mine)
- 8print(copy)
➜ yellow = the line about to run · ✓ = the line that just ran
The real words
- Every value is an object living somewhere in memory. A variable is a name that refers to an object.
- Mutable objects (lists, dictionaries, sets, most objects you’ll create) can be changed in place. Immutable ones (numbers, strings, tuples) can’t.
- An alias is a second name for the same object.
b = acreates an alias. a is basks “same object?” whilea == basks “same contents?”. Two clones are==but notis.- A clone is a new object with the same contents:
a[:],list(a),a.copy(). These make a shallow copy: if the list contains other lists, those inner lists are still shared.copy.deepcopy(a)copies all the way down. - Passing a list to a function passes a reference to it. If the function mutates it, the caller sees the change: a side effect. Sometimes that’s what you want (
lst.sort()); often it’s a surprise. - A classic trap: changing a list while looping over it (for example, removing items) skips elements. Loop over a copy instead:
for x in lst[:]:.
Why does it matter for immutable values? It doesn’t: if nothing can change, sharing is perfectly safe. That’s one reason immutability is so useful, and you’ll see it again in F03.
A function with no surprises
without_negatives(nums) should return a NEW list with the negative numbers removed, and must NOT change the list it was given. The starter has a bug: it mutates the caller's list. Fix it.
Predict the twins
a = [1, 2]; b = a; b = b + [3]. What is a?
Which line makes a real, independent copy of the list a?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?