user5597655
user5597655

Reputation:

Index error when creating 3d list in Python

I'm trying to create a 3d list but keep getting an error. Here is my code:

grid = []
for row in range(10):
    grid.append([])
    for column in range(10):
        grid[row].append([])
        for height in range(10):
            grid[column][row].append([])

This is the error I'm getting:

Traceback (most recent call last):
  File "/Users/kosay.jabre/Desktop/3dgrid.py", line 7, in <module>
    grid[column][row].append([])
IndexError: list index out of range

I don't see my mistake, can you help?

Upvotes: 0

Views: 809

Answers (3)

rassar
rassar

Reputation: 5680

You need to do grid[row][column] instead of grid[column][row].

Note that you can not do [[[[]]*10]*10]*10 as it will create aliases and when one list is modified they all get modified.

Upvotes: 2

PM 2Ring
PM 2Ring

Reputation: 55499

As rassar mentions, you got that indexing error because you reversed your indices. You should've had grid[row][column]instead of grid[column][row].

Here's the standard way to make a regular "multidimensional" list in Python.

def show(g):
    for block in g:
        for row in block:
            print(row)
        print()
    print()

grid = []
for z in range(3):
    block = []
    for y in range(4):
        row = [0] * 5
        block.append(row)
    grid.append(block)

show(grid)

for z in range(3):
    for y in range(4):
        for x in range(5):
            n = 100 * z + 10 * y + x + 111
            grid[z][y][x] = n

show(grid)

output

[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]

[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]

[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]


[111, 112, 113, 114, 115]
[121, 122, 123, 124, 125]
[131, 132, 133, 134, 135]
[141, 142, 143, 144, 145]

[211, 212, 213, 214, 215]
[221, 222, 223, 224, 225]
[231, 232, 233, 234, 235]
[241, 242, 243, 244, 245]

[311, 312, 313, 314, 315]
[321, 322, 323, 324, 325]
[331, 332, 333, 334, 335]
[341, 342, 343, 344, 345]

However, it's probably more common not to fully initialize a list like this. Rather, just create a base list and expand it as you need to. If you truly do need a proper multidimensional array, consider using Numpy.


BTW, it possible to condense those for loops into a nested list comprehension:

grid = [[[0] * 5 for y in range(4)] for z in range(3)]

It's certainly more compact than the previous code, and slightly faster, but a little less readable.

To improve readability we can spread it out over a few lines

grid = [
    [[0] * 5 for y in range(4)] 
        for z in range(3)
]

but it's still rather dense, and until you're very comfortable with Python I recommend using the traditional for loop method that I used earlier.

Upvotes: 1

Azat Ibrakov
Azat Ibrakov

Reputation: 11009

In your example when column is equal to 1 (and row is still 0) on the last line

grid[column][row].append([])

there is no element in grid which can be obtained with grid[1] and this causes error.

We can change last line to

grid[row][column].append([])

For this task we can also use list comprehension like

grid = [[[[]
          for height in range(10)]
         for column in range(10)]
        for row in range(10)]

why not @rassar suggestion? because it will duplicate the same lists and it is not what you want i suppose (more in this thread):

>>>grid = [[[[]
          for height in range(10)]
         for column in range(10)]
        for row in range(10)]
>>>grid2 = [[[[]] * 10] * 10] * 10
>>>grid[0][0][0].append(1)
>>>grid2[0][0][0].append(1)
>>>grid
 [[[[1], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]],
  [[[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]],
  [[[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]],
  [[[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]],
  [[[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]],
  [[[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]],
  [[[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]],
  [[[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]],
  [[[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]],
  [[[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []], [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []],
   [[], [], [], [], [], [], [], [], [], []]]]
>>>grid2
[[[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]],
 [[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]],
 [[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]],
 [[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]],
 [[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]],
 [[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]],
 [[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]],
 [[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]],
 [[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]],
 [[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]],
  [[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]]]

finally you should take a look at NumPy which is all about multidimensional arrays

Upvotes: 1

Related Questions