Is there any reason why the below happens? In both cases, we defined grid as 3 by 3 array of zeroes, but when we change middle value to 2, three values are changed to 2 whereas in the second case, only one value was changed to 2.
grid=[[0]*3]*3
grid[1][1]=2
print(grid)
[[0, 2, 0], [0, 2, 0], [0, 2, 0]]
grid=[[0 for i in range(3)] for i in range(3)]
grid[1][1]=2
print(grid)
[[0, 0, 0], [0, 2, 0], [0, 0, 0]]