I’ve never worked with 2D or 3D arrays before but i’m trying to make a maze.
In my snippet, squares is a list with each instance of a cell (so in a 3×4 maze, there would be 12 instances in squares)
I am then trying to append to row, a list of all the squares in a row, so row[0] would contain the first four square instances, row[1] would be the next four, etc.
the row[x].append(squares[y+z]) throws the IndexError, i’m guessing it’s the row[x] part, but i’m not sure what to do to fix it. I tried using extend instead of append.
numberOfRows = 3
numberOfColumns = 4
z = 0
for x in range(numberOfRows):
for y in range(numberOfColumns):
row[x].append(squares[y+z])
z += 4
If I’m guessing it right, you want:
i.e., you were only missing the
rowdefinition.EDIT:
After reading OP’s comments, it seems that considering the following alternative is worth for the situation:
So you don’t create all the lists in
rowbeforehand.