Making copies
🌍 In real life. If you want your own copy of a friend's notes, you photocopy them; writing your name on their notebook doesn't give you a second notebook. And if the notes have a folded map tucked inside, a quick photocopy of the pages still leaves only one map, shared between you.
Python copies work the same way, including the folded-map surprise.
The idea in one sentence: a.copy(), list(a) and a[:] make a new outer list, so changing the copy leaves the original alone; copy.deepcopy(a) also copies everything inside.
Its words:
- copy: a new object with the same values
- shallow copy: a new outer list whose items are still the same objects as before
- deep copy: copies inner lists too, all the way down
Step by step, for c = a.copy(), c.append(9):
a.copy()makes a new list holding the same items asa.cpoints at that new list, not ata's list.c.append(9)changes only the new list, soastays[1, 2].
a = [1, 2]
c = a.copy()
c.append(9)
print(a)
print(c)A famous trap: [[0] * 2] * 2 doesn't make two rows. It makes one row and two arrows to it, so changing one row changes "both". Build the rows with a comprehension instead, so each one is a new list:
grid = [[0] * 2] * 2
grid[0][0] = 5
print(grid)
fixed = [[0] * 2 for _ in range(2)]
fixed[0][0] = 5
print(fixed)a.copy() is a shallow copy: it makes a new outer list, but any lists inside are still shared. copy.deepcopy(a) copies everything, all the way down. (import copy loads Python's copy tools; Chapter 19 is all about importing.)
import copy
a = [[1, 2], [3]]
shallow = a.copy()
deep = copy.deepcopy(a)
a[0].append(9)
print(shallow)
print(deep)What it costs. = is free: it only moves a tag. A shallow copy walks the list once, so it takes time and memory in proportion to the length. A deep copy walks everything inside too, which is the most expensive, so use it only when inner lists really must be separate.
Mistakes and edge cases.
- = instead of a copy. It only makes an alias.
- Building a grid with *.
[[0] * 3] * 3makes one row with three arrows to it (see the bug question). Use a comprehension. - Trusting a shallow copy with nested lists.
a.copy()still shares the inner lists; usecopy.deepcopywhen you'll change them. - Forgetting import copy.
copy.deepcopy(a)without it givesNameError: name 'copy' is not defined.
Quiz
What does this print?
a = [1, 2] b = a[:] b.append(3) print(a)What does this print?
g = [[0]] * 2 g[0].append(1) print(g)What does this print?
a = [1] b = list(a) print(a == b, a is b)Which line gives
ba separate copy of the lista?- b = a.copy()
- b = a
- b is a
What does this print?
a = [[1], [2]] b = a.copy() b[0].append(5) print(a[0])Only the first row should get a 1, but every row does. Which line causes it?
grid = [[0] * 3] * 3 grid[0][0] = 1 print(grid)
Coding tasks
Start with
a = [1, 2, 3]. Makeba copy of it, append 4 tob, then printaand thenb.Ask for a number of rows, then a number of columns. Make a grid of zeros that size, with separate rows. Set the top-left item to 1 and print the grid.
Ask for whole numbers on one line, then for one more number
val. Remove every copy ofvalfrom the list itself (keep the others in order). Print the list, then how many numbers are left.