I changed one cell in a Python grid, and all three rows changed.
This bug shows up in games, matrices and dynamic-programming code all the time, and the line that causes it looks completely innocent. Every output below is from Python 3.15.
Video version:
The bug
>>> grid = [[0] * 3] * 3 >>> grid[0][0] = 1 >>> grid [[1, 0, 0], [1, 0, 0], [1, 0, 0]] One assignment, three changed rows. Here's the same thing in a tic-tac-toe board:
board = [["."] * 3] * 3 board[1][1] = "X" for row in board: print(" ".join(row)) . X . . X . . X . One move, a whole column of X's. If your game logic seemed haunted, this is probably why.
Why: it's one list, three times
Multiplying a list doesn't copy what's inside it. It repeats references to the same objects. So the outer list holds the same inner list three times:
>>> [id(row) for row in grid] [4376155840, 4376155840, 4376155840] >>> grid[0] is grid[1] True All three ids are identical. There is only one row, and you're looking at it three times. Writing into "row 0" writes into that one shared list.
Then why does [0] * 3 work?
[0] * 3 also repeats references, to the integer 0. But integers are immutable: you can't change a 0. row[0] = 5 replaces the item in that slot, it doesn't modify the 0 object:
>>> row = [0] * 3 >>> row[0] = 5 >>> row [5, 0, 0] Lists are mutable. grid[0][0] = 1 modifies the inner list, and that inner list is shared by every slot.
So * is safe for immutable items (numbers, strings, None, tuples) and a trap for mutable ones (lists, dicts, sets, objects).
The fix: a list comprehension
grid = [[0] * 3 for _ in range(3)] The expression [0] * 3 now runs once per row, so every row is a new list:
>>> grid[0][0] = 1 >>> grid [[1, 0, 0], [0, 0, 0], [0, 0, 0]] >>> grid[0] is grid[1] False Rule of thumb: * n on the inside is fine, never on the outside of a list of lists.
Same trap: dict.fromkeys()
>>> groups = dict.fromkeys(["admins", "users"], []) >>> groups["admins"].append("ann") >>> groups {'admins': ['ann'], 'users': ['ann']} Every key gets the same list object, so adding Ann to admins also made her a user. Use a dict comprehension, which creates a new list per key:
>>> groups = {k: [] for k in ["admins", "users"]} >>> groups["admins"].append("ann") >>> groups {'admins': ['ann'], 'users': []} (Or collections.defaultdict(list).)
Same trap: .copy() is shallow
>>> a = [[1, 2], [3, 4]] >>> b = a.copy() >>> b[0].append(99) >>> a [[1, 2, 99], [3, 4]] .copy() (and list(a), and a[:]) copies only the outer list. The inner lists are still shared. For nested data, use copy.deepcopy():
>>> import copy >>> c = copy.deepcopy(a) >>> c[0].append(7) >>> a [[1, 2, 99], [3, 4]] # untouched >>> c [[1, 2, 99, 7], [3, 4]] Recap
| Code | What you get |
|---|---|
[[0] * 3] * 3 | ❌ one row, three references |
[[0] * 3 for _ in range(3)] | ✅ three separate rows |
dict.fromkeys(keys, []) | ❌ every key shares one list |
{k: [] for k in keys} | ✅ a new list per key |
a.copy() on nested lists | ❌ inner lists shared |
copy.deepcopy(a) | ✅ fully independent copy |
When in doubt, check with is or id(). If two things have the same id, they're the same object.
This is the same root cause as Python's mutable default argument bug: one mutable object, shared where you expected copies.
Has this one ever bitten you? 👇
I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.