Python 3D Academyবাংলা

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):

  1. a.copy() makes a new list holding the same items as a.
  2. c points at that new list, not at a's list.
  3. c.append(9) changes only the new list, so a stays [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] * 3 makes 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; use copy.deepcopy when you'll change them.
  • Forgetting import copy. copy.deepcopy(a) without it gives NameError: name 'copy' is not defined.

Quiz

  1. What does this print?

    a = [1, 2]
    b = a[:]
    b.append(3)
    print(a)
  2. What does this print?

    g = [[0]] * 2
    g[0].append(1)
    print(g)
  3. What does this print?

    a = [1]
    b = list(a)
    print(a == b, a is b)
  4. Which line gives b a separate copy of the list a?

    • b = a.copy()
    • b = a
    • b is a
  5. What does this print?

    a = [[1], [2]]
    b = a.copy()
    b[0].append(5)
    print(a[0])
  6. 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

  1. Start with a = [1, 2, 3]. Make b a copy of it, append 4 to b, then print a and then b.

  2. 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.

  3. Ask for whole numbers on one line, then for one more number val. Remove every copy of val from the list itself (keep the others in order). Print the list, then how many numbers are left.