What is the output of the following Python code? grid = * 2] * 2; grid = 1; print(grid)
Correct answer D. [[1, 0], [1, 0]]
Explanation
Multiplying a list copies references, not objects, so both rows of grid refer to the same inner list. Changing grid[0][0] therefore appears in both rows. To build independent rows use a comprehension such as [[0] * 2 for _ in range(2)], which creates a new inner list on each iteration.
Answer checked How we write and check questions
Discussion