TryBugHunt

Changing one row of a 2D list changes them all

Changing one row of a 2D list changes them all in Python means [[0] * cols] * rows repeats a reference to one inner list. Every row is the same object, so one assignment shows up everywhere.

PythonPattern: Mutation and copying bugs

How do I fix Changing one row of a 2D list changes them all?

In Python, grid = [[0] * 3] * 3 makes three references to one inner list, so changing grid[0][0] changes every row. The * operator copies references, not lists. Build each row separately with a list comprehension: grid = [[0] * 3 for _ in range(3)]. To copy a grid, use copy.deepcopy(grid).

  1. 1. Look for [[...]] * nAny [[...]] * n or [{}] * n repeats one object. Replace it with a comprehension using for _ in range(n).
  2. 2. Compare row idsPrint id(grid[0]) and id(grid[1]). The same number means the rows are one list.
  3. 3. Shallow copiesgrid[:] and grid.copy() share the inner lists. Use copy.deepcopy(grid) for a fully separate copy.

What does Changing one row of a 2D list changes them all mean?

Multiplying a list copies references, not values. For numbers that is harmless, because numbers cannot be changed in place, but for an inner list it means grid[0] and grid[1] are one list seen through two names.

Why does Changing one row of a 2D list changes them all happen?

1. Building a grid with *

The inner list is created once and shared.

Breaks

grid = [[0] * 3] * 3
grid[0][0] = 1   # every row changes

Works

grid = [[0] * 3 for _ in range(3)]

2. Repeating a dict or list default

Same trap with any mutable value.

Breaks

rows = [{}] * 5

Works

rows = [{} for _ in range(5)]

3. Copying a grid with a shallow copy

copy() and [:] copy the outer list only.

Breaks

backup = grid[:]

Works

import copy
backup = copy.deepcopy(grid)

How to find it in your own code

Build each inner list separately with a comprehension. When a change appears in several places at once, print id() of the parts: matching ids mean shared objects.

Practise mutation and copying bugs

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.