0

I have a program that receives a positive integer labelled n, and will create an n * n array all consisting zeros (e.g. if n = 3, a 3x3 array is created all containing zeros)

row = [0] * n
grid = [row] * n

Whilst this does successfully generate the n*n array, when I assign one element a value, it changes multiple values of the array instead of only that one element. In the code below, assume n = 3

>>> print(grid)
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

grid[0][0] = 1
>>> print(grid)
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]

I want the change to only affect the 1st value in the 1st array so that

grid = [1,0,0],[0,0,0],[0,0,0]

How can I achieve this?

ZaBoss786
  • 1
  • 1

0 Answers0