Sub topic 3
Creating 2D Arrays, and Why [[0]*3]*2 Shares Rows
This chapter assumes you know Python lists, zero-based indexing, and how to read a list comprehension. You should also know how to index a 2D array with grid[row][col]. Here you will learn the standard ways to create a 2D array and the one mistake that catches almost everyone.
The goal: independent rows
In Python, a 2D array is a list of lists. Each inner list is one row. For a grid to behave correctly, every row must be its own separate list object. If two rows are the same object, changing one changes the other.
A literal for small grids
For a small, fixed grid, write the values out.
grid = [
[1, 2, 3],
[4, 5, 6],
]
print(grid[1][2]) # 6Each inner [...] creates a new list, so the rows are independent. This approach does not scale to a 100 by 100 grid, so you need a way to build rows in a loop.
The shared-row pitfall
The short form [[0] * 3] * 2 looks right but is wrong.
bad = [[0] * 3] * 2
bad[0][0] = 1
print(bad) # [[1, 0, 0], [1, 0, 0]] (both rows are the same list)The reason is how * works on a list. [0] * 3 builds a new list with three zeros, which is fine. The outer * 2 does not copy that inner list. It repeats a reference to it. The outer list ends up holding two references to one inner list.
When you write bad[0][0] = 1, Python follows bad[0] to the shared row and changes its first cell. Reading bad[1][0] follows the same reference, so it shows the change.
You can confirm the sharing with is, which tests whether two names refer to the same object.
bad = [[0] * 3] * 2
print(bad[0] is bad[1]) # True
good = [[0] * 3 for _ in range(2)]
print(good[0] is good[1]) # FalseThe fix: build each row separately
A list comprehension runs the inner expression once per row. Each run creates a new list.
good = [[0] * 3 for _ in range(2)]
good[0][0] = 1
print(good) # [[1, 0, 0], [0, 0, 0]]The _ is a conventional name for a loop variable you do not use. Here [0] * 3 is safe because the elements are integers, which cannot be changed in place. The problem only appears when the repeated item is a mutable object such as a list.
A reusable helper
Wrap the pattern in a function so you do not have to remember it.
def make_grid(rows, cols, fill=0):
return [[fill] * cols for _ in range(rows)]
g = make_grid(2, 3)
g[1][1] = 9
print(g) # [[0, 0, 0], [0, 9, 0]]
print(len(g), len(g[0])) # 2 3The shape here is rows by cols, so len(g) gives the row count and len(g[0]) gives the column count.
The same trap applies to fill. If you pass a mutable value such as [], every cell in a row shares it. Keep fill to numbers, strings, booleans, or None.
Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| `[[0] * c] * r` | Changing one row changes all rows | Use `[[0] * c for _ in range(r)]` |
| Swapping `rows` and `cols` | `IndexError` or a transposed grid | Rows come first: `[... for _ in range(rows)]` |
| `[[]] * r` then `append` | All rows grow together | Use `[[] for _ in range(r)]` |
Exercise
After running m = [[0] * 2] * 3 and then m[2][1] = 7, what does m contain, and why?
Show answer
m is [[0, 7], [0, 7], [0, 7]]. The outer * 3 repeats a reference to one inner list, so all three rows are the same object and the change shows in each. Building it with [[0] * 2 for _ in range(3)] would give [[0, 0], [0, 0], [0, 7]].
Exercise
Write one line that creates a 4 by 5 grid (4 rows, 5 columns) of zeros with independent rows.
Show answer
grid = [[0] * 5 for _ in range(4)]