Sub topic 6
Row-Major Versus Column-Major Order
This chapter assumes you can write nested for loops, index a Python list with zero-based indices, and build a 2D array as a list of lists. Earlier chapters showed how to visit every cell with an outer loop over rows and an inner loop over columns. Here you will see that this is one of two natural visiting orders, and why the choice matters.
Two ways to walk a grid
A 2D array has rows rows and cols columns. Any full traversal visits each of the rows * cols cells once. The only question is which index changes fastest.
- Row-major order finishes one row before starting the next. The column index changes fastest.
- Column-major order finishes one column before starting the next. The row index changes fastest.
For the grid [[1, 2, 3], [4, 5, 6]], the two orders produce different sequences:
| Order | Visit sequence |
|---|---|
| Row-major | 1, 2, 3, 4, 5, 6 |
| Column-major | 1, 4, 2, 5, 3, 6 |
Writing both loops
The order is decided entirely by which loop is on the outside. The loop body is the same in both versions.
grid = [[1, 2, 3], [4, 5, 6]]
rows, cols = len(grid), len(grid[0])
print("row-major:", end=" ")
for r in range(rows): # outer loop: rows
for c in range(cols): # inner loop: columns
print(grid[r][c], end=" ")
print()
print("column-major:", end=" ")
for c in range(cols): # outer loop: columns
for r in range(rows): # inner loop: rows
print(grid[r][c], end=" ")
print()The output is:
row-major: 1 2 3 4 5 6
column-major: 1 4 2 5 3 6 Both loops do the same amount of work and give the same result for order-independent tasks such as summing every cell. Order matters when the result depends on sequence, for example when you print cells, fill a grid with counting numbers, or read a file that was written in one order.
Why the order can affect speed
Computer memory is a single line of slots. A 2D array must be flattened into that line. A row-major layout stores row 0, then row 1, and so on. The cell (r, c) sits at flat position r * cols + c, a formula covered in a later chapter.
Memory hardware is fastest when you read neighboring slots one after another, because it loads nearby data into a fast cache together. In a row-major layout, a row-major traversal reads neighbors, and a column-major traversal jumps cols slots on every step.
Languages differ on which layout they use:
- C, C++, Java, Python lists of lists, and NumPy by default store rows together (row-major).
- Fortran, MATLAB, and R store columns together (column-major).
Match your loops to the storage layout when performance matters. With Python lists the effect is small, because each row is a separate list object. It becomes large with contiguous numeric arrays in other languages.
Common mistakes
- Swapping the bounds. In a column-major loop the outer range is
colsand the inner range isrows. Usingrowsfor both works only on square grids and raisesIndexErroror skips cells otherwise. - Swapping the indices. Always write
grid[r][c], with the row first, even when the column loop is on the outside. - Assuming order never matters. A fill such as "number the cells 1, 2, 3, ..." produces a different grid in each order.
Exercise
A grid has 3 rows and 4 columns. You number cells 1 to 12 in column-major order, starting at the top-left. What number lands at row 1, column 2 (zero-based)?
Show answer
In column-major order the numbers run down each column of 3 cells. Columns 0 and 1 hold 1 to 6. Column 2 holds 7, 8, 9 in rows 0, 1, 2, so row 1, column 2 holds 8.