0

This code block is giving me unexpected results, and I am not sure why:

grid = [[0] * cols] * rows
grid[0][1] = 1
print(grid) # [[0, 1], [0, 1]]

I expected the grid to be [[0, 1], [0, 0]], not [[0, 1], [0, 1]]. What causes grid[1][1] to be mutated as well?

Thank you.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Andre
  • 530
  • 3
  • 15

0 Answers0