Creating Arrays
Reshape 1D to 2D
Six flat values placed into a 2×3 grid using row-major index math. The trace
shows r and c computed from each flat index i via i // cols and
i % cols, and grid filling left-to-right, top-to-bottom.
By hand
Pre-initialise a 2×3 grid of zeros. For each flat index i, compute the
target row r = i // cols and column c = i % cols, then write flat[i]
into grid[r][c]. The grid shows the fill progressing across the first row
before moving to the second.
naive.py
Replay: real traced execution (multi-file project)
flat = [1, 2, 3, 4, 5, 6]
rows, cols = 2, 3
grid = [[0, 0, 0], [0, 0, 0]]
for i in range(rows * cols):
r = i // cols
c = i % cols
grid[r][c] = flat[i]
print('RESULT:', grid)
flat ← [1, 2, 3, 4, 5, 6]
1flat = [1, 2, 3, 4, 5, 6]2rows, cols = 2, 3values this step[1, 2, 3, 4, 5, 6]flatcols ← 3, rows ← 2
1flat = [1, 2, 3, 4, 5, 6]2rows, cols = 2, 33grid = [[0, 0, 0], [0, 0, 0]]values this step3cols2rowsgrid ← [[0, 0, 0], [0, 0, 0]]
2rows, cols = 2, 33grid = [[0, 0, 0], [0, 0, 0]]4for i in range(rows * cols):values this step[[0, 0, 0], [0, 0, 0]]gridi ← 0
3grid = [[0, 0, 0], [0, 0, 0]]4for i in range(rows * cols):5 r = i // colsvalues this step0ir ← 0
4for i in range(rows * cols):5 r = i // cols6 c = i % colsvalues this step0rc ← 0
5r = i // cols6c = i % cols7grid[r][c] = flat[i]values this step0cgrid ← [[1, 0, 0], [0, 0, 0]]
6 c = i % cols7 grid[r][c] = flat[i]8print('RESULT:', grid)values this step[[0, 0, 0], [0, 0, 0]] → [[1, 0, 0], [0, 0, 0]]gridi ← 1
3grid = [[0, 0, 0], [0, 0, 0]]4for i in range(rows * cols):5 r = i // colsvalues this step0 → 1ir = i // cols
4for i in range(rows * cols):5 r = i // cols6 c = i % colsc ← 1
5r = i // cols6c = i % cols7grid[r][c] = flat[i]values this step0 → 1cgrid ← [[1, 2, 0], [0, 0, 0]]
6 c = i % cols7 grid[r][c] = flat[i]8print('RESULT:', grid)values this step[[1, 0, 0], [0, 0, 0]] → [[1, 2, 0], [0, 0, 0]]gridi ← 2
3grid = [[0, 0, 0], [0, 0, 0]]4for i in range(rows * cols):5 r = i // colsvalues this step1 → 2ir = i // cols
4for i in range(rows * cols):5 r = i // cols6 c = i % colsc ← 2
5r = i // cols6c = i % cols7grid[r][c] = flat[i]values this step1 → 2cgrid ← [[1, 2, 3], [0, 0, 0]]
6 c = i % cols7 grid[r][c] = flat[i]8print('RESULT:', grid)values this step[[1, 2, 0], [0, 0, 0]] → [[1, 2, 3], [0, 0, 0]]gridi ← 3
3grid = [[0, 0, 0], [0, 0, 0]]4for i in range(rows * cols):5 r = i // colsvalues this step2 → 3ir ← 1
4for i in range(rows * cols):5 r = i // cols6 c = i % colsvalues this step0 → 1rc ← 0
5r = i // cols6c = i % cols7grid[r][c] = flat[i]values this step2 → 0cgrid ← [[1, 2, 3], [4, 0, 0]]
6 c = i % cols7 grid[r][c] = flat[i]8print('RESULT:', grid)values this step[[1, 2, 3], [0, 0, 0]] → [[1, 2, 3], [4, 0, 0]]gridi ← 4
3grid = [[0, 0, 0], [0, 0, 0]]4for i in range(rows * cols):5 r = i // colsvalues this step3 → 4ir = i // cols
4for i in range(rows * cols):5 r = i // cols6 c = i % colsc ← 1
5r = i // cols6c = i % cols7grid[r][c] = flat[i]values this step0 → 1cgrid ← [[1, 2, 3], [4, 5, 0]]
6 c = i % cols7 grid[r][c] = flat[i]8print('RESULT:', grid)values this step[[1, 2, 3], [4, 0, 0]] → [[1, 2, 3], [4, 5, 0]]gridi ← 5
3grid = [[0, 0, 0], [0, 0, 0]]4for i in range(rows * cols):5 r = i // colsvalues this step4 → 5ir = i // cols
4for i in range(rows * cols):5 r = i // cols6 c = i % colsc ← 2
5r = i // cols6c = i % cols7grid[r][c] = flat[i]values this step1 → 2cgrid ← [[1, 2, 3], [4, 5, 6]]
6 c = i % cols7 grid[r][c] = flat[i]8print('RESULT:', grid)values this step[[1, 2, 3], [4, 5, 0]] → [[1, 2, 3], [4, 5, 6]]gridfor i in range(rows * cols):
3grid = [[0, 0, 0], [0, 0, 0]]4for i in range(rows * cols):5 r = i // colsstdout ← RESULT: [[1, 2, 3], [4, 5, 6]]
7 grid[r][c] = flat[i]8print('RESULT:', grid)values this stepRESULT: [[1, 2, 3], [4, 5, 6]]stdout
With NumPy
np.array(flat).reshape(2, 3) creates a 1-D array and reinterprets its memory
as 2 rows of 3 columns without copying. The snapshot shows the array's shape,
element type, and values as a nested Python list.
library.py
import numpy as np
flat = [1, 2, 3, 4, 5, 6]
arr = np.array(flat).reshape(2, 3)
print('shape:', arr.shape)
print('dtype:', arr.dtype)
print('values:', arr.tolist())
print('RESULT:', arr.tolist())
shape: (2, 3)
dtype: int64
values: [[1, 2, 3], [4, 5, 6]]
RESULT: [[1, 2, 3], [4, 5, 6]]
Implementation notes
- NumPy uses row-major (C) order by default: elements are stored in memory
left-to-right within each row, then row by row. The index formulas
r = i // colsandc = i % colsimplement exactly this layout. reshapereturns a view of the original data whenever possible — no copy is made. Modifying the reshaped array also modifies the original. Pass.reshape(...).copy()to force an independent copy.- One dimension may be given as
-1and NumPy infers it:np.arange(6).reshape(2, -1)gives the same 2×3 result. - This operation is the column-parallel inverse of
zip-columns-to-rowsinpython-data-basics/ch08: where that lesson assembles separate column lists into row tuples, reshape takes a flat sequence and partitions it into rows. - Shape, dtype, and values are shown explicitly here because
ndarray.__repr__output varies with NumPy version and print options.