Practice Question: Shallow Copy vs Deep Copy
What is the output of the following code snippet?
grid = [[0] * 3] * 3
grid[0][0] = 3
print(grid)Answer
This prints:
[[3, 0, 0], [3, 0, 0], [3, 0, 0]]This happens because the list was created using a shallow copy, and the nested elements are mutable; therefore, they share the same reference (memory address). As a result, modifying one inner list changes all of them in the same way.