loop
Lists in advanced applications

Module 3 · Lists in advanced applications · Lesson 3 of 3

Multidimensional lists

Here's the beautiful part: nothing stops at two dimensions. If a row can be a list, and a grid can be a list of rows, then a stack of grids is just a list of grids. Each extra [ ] you add is one more dimension — one more list wrapped around the last.

cube[1][0][2]
[0]
123456
[1]
789101112
[2]
131415161718

Reading a value follows the layers from the outside in: cube[1] picks a layer, cube[1][0] a row in it, cube[1][0][2] a single cell. The pattern never changes — you just chain one more index per dimension.

Why bother? Because real data has shape. Think of a month of hourly temperatures (week, day, hour), the cells of a 3-D game world, or a stack of image layers. A comprehension builds the whole structure at once.

Run it yourselfruns in your browser · Python
main.py

And to visit every value, you nest one loop per dimension — three dimensions, three for loops. It looks like a lot, but it's the same idea from the very first list, repeated.

Run it yourselfruns in your browser · Python
main.py

That's the whole of lists, start to finish: one box, a row of boxes, a grid, and a structure of any depth you need — all from the same simple idea of putting values, and lists, inside a list.