Rows and Columns in Nested Lists
A nested list commonly uses one index to choose a row and another to choose a value inside it.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Read the coordinates
For rows [[2, 4, 6], [1, 3, 5]], accessing row one then column two produces 5. Clarify row-first order when coordinates come from a display that uses horizontal then vertical position. Both offsets follow zero-based indexing.
Check each row length
In [[2, 4], [1]], row one has no entry at column one. If an algorithm requires a rectangular grid, validate every row up front. This belongs in the algorithm's preconditions, especially if empty rows are possible.[1]
Check coordinates against the correct dimension
For a grid with three rows and five columns, coordinate (2, 4) is valid when written as row followed by column. Swapping the components yields an invalid row index four. On a square grid swapping them can silently retrieve a different element, so avoid test grids with symmetric dimensions and data. Converting display labels that start at one requires subtracting one from each component. See zero-based indexing.
Derive the offset and check the inverse
For row-major storage with width \(w>0\), an allowed coordinate \((r,c)\) maps to \(rw+c\). For width five, \((2,4)\) maps to fourteen. Integer quotient and remainder of fourteen by five recover row two and column four. This formula assumes all rows have the same length. A ragged collection needs per-row lengths or separate storage for each row. See row-major flattening and test the last column and the first column of adjacent rows.