0

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?

3 Answers3

2

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.

rassar
  • 5,412
  • 3
  • 25
  • 41
  • 1
    -1 While your explanation for the OP's error is correct, your solution is wrong. Doing `grid = [[[[]]*10]*10]*10` will not create new list in `grid`. Rather, it will only create new _references_ to the same list objects in `grid`. Thus, you cannot set grid positions to unique values. See [here](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) for more information on the topic. – Christian Dean Jun 13 '17 at 01:55
  • What @ChristianDean said. Try `grid = [[[[]]*4]*4]*4; grid[1][1][1].append(1); print(grid)` – PM 2Ring Jun 13 '17 at 01:57
1

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

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
  • While you _could_ use a nested list comprehension, in this case it is a much better option to simply use a regular for-loop. Your are not really gaining much by using them, and your only adding to the visual noise. – Christian Dean Jun 13 '17 at 02:03
1

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.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • What are the advantages of using Numpy for multidimensional arrays? Can I store Python objects inside Numpy arrays? –  Jun 13 '17 at 02:07
  • @Kos One of the biggest advantages is speed. NumPy arrays are implemented in C and are much faster than normal Python arrays. A downside however, is that all data in the arrays must be homogeneous. – Christian Dean Jun 13 '17 at 02:08
  • @Kos: yes, you can create `NumPy` arrays from `list`s/`tuple`s – Azat Ibrakov Jun 13 '17 at 02:11
  • 1
    @Kos Yes, you _can_ store Python objects in Numpy arrays, but they're designed to store native machine numeric types (eg various sizes of signed and unsigned integers and floating point numbers), on which Numpy can operate at the speed of compiled code. But before you get into Numpy it's a good idea to master the basics of core Python. – PM 2Ring Jun 13 '17 at 02:15