-1

I want to create a grid containing a pair of coordinates of the cell in question as such:

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

And so on. When I do the following:

colRange = 2
rowRange = 3
for column in range(colRange):
   for row in range(rowRange):
       grid[row][column] = [row,column]

I expected to get the outcome of above, but instead I receive this:

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

and so on. So it inputs the maximum row index into the first coordinate, for every row and column.

I would love if someone could explain what is happening here.

1 Answers1

0

Try:

grid = [[[i,j] for j in range(colRange)] for i in range(rowRange)]

>>> grid
[[[0, 0], [0, 1]], [[1, 0], [1, 1]], [[2, 0], [2, 1]]]
not_speshal
  • 22,093
  • 2
  • 15
  • 30