What are 2D Lists?
2D lists (or "nested lists") are simply lists that contain other lists as their elements. These are not only beneficial for storing complex sets of data, but also for representing things like grids and matrices.
2D Lists as Grids
There are two ways that one can organize a 2D list to make a grid: as a single outer column where each inner list represents a complete row, or as a single outer row where each inner list represents a complete column. Each of these approaches has its own pros and cons.
list # U
list # P
The approach where each inner list is a complete row may at first seem like the more natural or intuitive strategy. It certainly makes it easier to transform a text file into a grid, since each row can be read in one by one from the file and immediately transformed into the corresponding list.
However, this approach also has a major drawback: the final grid must be accessed in reverse-coordinate order, using grid[y_index][x_index] notation.
For example, let's say I am creating the letter grid shown above, which has a width of 6 and a height of 4. If I wish to access the bottom-right corner (the letter X), I would need to use the syntax grid. This is because the first coordinate accesses the column position, since the outer list is a column, and the second coordinate accesses the row position, since the inner lists are rows.
In order to access a grid position using correct coordinate order, using grid[x_index][y_index] notation, it must be organized as a single outer row of complete inner columns. In the same example, the bottom-right corner of a 6 x 4 grid is now grid, because the row position is accessed first. This allows for a much more intuitive access of individual grid cells, though it does make initializing the grid in-line more complicated (as shown below).
Because the dual-index notation for accessing grid positions is very common, grids in our TechSmart courses are generally organized in this second fashion: as a single outer list representing a row and each inner list representing a complete column. This allows students to use the same notation they are used to in graphical programs: the horizontal (x) location is specified first, followed by the vertical (y) location.
However, Python is flexible: depending on the need of your specific program, it's entirely possible to organize your grids whichever way you choose.