I want to create a 2d array from integer values initially set to 0.
Here's how I could do it:
grid = [[0] * width] * height
But it just creates each line as a link to the original, so something like
grid[0][1] = 1
will change all grid values [n] [1].
This is my decision:
grid = [[0 for x in range(width)] for y in range(height)]
It feels wrong, is there an even more pythonic way to do this?
EDIT: Bonus question, if the grid size never changes and contains only integers, should something like a tuple or array.array be used instead?
source
share