Indexing and Slicing
Index Element
Four corner cells of a 2×3 grid accessed by row and column index. The trace
walks a list of [i, j] coordinates, extracting i and j from each, then
reads grid[i][j] to collect the four values.
By hand
Store the target coordinates as a list of two-element lists. For each coord,
extract i = coord[0] and j = coord[1], then read grid[i][j].
naive.py
Replay: real traced execution (multi-file project)
grid = [[1, 2, 3], [4, 5, 6]]
coords = [[0, 0], [0, 2], [1, 0], [1, 2]]
cells = []
for coord in coords:
i = coord[0]
j = coord[1]
cells.append(grid[i][j])
print('RESULT:', cells)
grid ← [[1, 2, 3], [4, 5, 6]]
1grid = [[1, 2, 3], [4, 5, 6]]2coords = [[0, 0], [0, 2], [1, 0], [1, 2]]values this step[[1, 2, 3], [4, 5, 6]]gridcoords ← [[0, 0], [0, 2], [1, 0], [1, 2]]
1grid = [[1, 2, 3], [4, 5, 6]]2coords = [[0, 0], [0, 2], [1, 0], [1, 2]]3cells = []values this step[[0, 0], [0, 2], [1, 0], [1, 2]]coordscells ← []
2coords = [[0, 0], [0, 2], [1, 0], [1, 2]]3cells = []4for coord in coords:values this step[]cellscoord ← [0, 0]
3cells = []4for coord in coords:5 i = coord[0]values this step[0, 0]coordi ← 0
4for coord in coords:5 i = coord[0]6 j = coord[1]values this step0ij ← 0
5i = coord[0]6j = coord[1]7cells.append(grid[i][j])values this step0jcells ← [1]
6 j = coord[1]7 cells.append(grid[i][j])8print('RESULT:', cells)values this step[] → [1]cellscoord ← [0, 2]
3cells = []4for coord in coords:5 i = coord[0]values this step[0, 0] → [0, 2]coordi = coord[0]
4for coord in coords:5 i = coord[0]6 j = coord[1]j ← 2
5i = coord[0]6j = coord[1]7cells.append(grid[i][j])values this step0 → 2jcells ← [1, 3]
6 j = coord[1]7 cells.append(grid[i][j])8print('RESULT:', cells)values this step[1] → [1, 3]cellscoord ← [1, 0]
3cells = []4for coord in coords:5 i = coord[0]values this step[0, 2] → [1, 0]coordi ← 1
4for coord in coords:5 i = coord[0]6 j = coord[1]values this step0 → 1ij ← 0
5i = coord[0]6j = coord[1]7cells.append(grid[i][j])values this step2 → 0jcells ← [1, 3, 4]
6 j = coord[1]7 cells.append(grid[i][j])8print('RESULT:', cells)values this step[1, 3] → [1, 3, 4]cellscoord ← [1, 2]
3cells = []4for coord in coords:5 i = coord[0]values this step[1, 0] → [1, 2]coordi = coord[0]
4for coord in coords:5 i = coord[0]6 j = coord[1]j ← 2
5i = coord[0]6j = coord[1]7cells.append(grid[i][j])values this step0 → 2jcells ← [1, 3, 4, 6]
6 j = coord[1]7 cells.append(grid[i][j])8print('RESULT:', cells)values this step[1, 3, 4] → [1, 3, 4, 6]cellsfor coord in coords:
3cells = []4for coord in coords:5 i = coord[0]stdout ← RESULT: [1, 3, 4, 6]
7 cells.append(grid[i][j])8print('RESULT:', cells)values this stepRESULT: [1, 3, 4, 6]stdout
With NumPy
np.array(grid) creates a 2-D array. The comma syntax a[i, j] reads the
element at row i, column j directly — no intermediate row object needed.
The snapshot shows the array then four individual cell reads.
library.py
import numpy as np
grid = [[1, 2, 3], [4, 5, 6]]
a = np.array(grid)
print('shape:', a.shape)
print('dtype:', a.dtype)
print('values:', a.tolist())
print('a[0, 0]:', int(a[0, 0]))
print('a[0, 2]:', int(a[0, 2]))
print('a[1, 0]:', int(a[1, 0]))
print('a[1, 2]:', int(a[1, 2]))
print('RESULT:', [int(a[0, 0]), int(a[0, 2]), int(a[1, 0]), int(a[1, 2])])
shape: (2, 3)
dtype: int64
values: [[1, 2, 3], [4, 5, 6]]
a[0, 0]: 1
a[0, 2]: 3
a[1, 0]: 4
a[1, 2]: 6
RESULT: [1, 3, 4, 6]
Implementation notes
- Python's
a[i][j]first retrieves the rowa[i](a full Python list), then indexes into it with[j]. NumPy'sa[i, j]is a single operation that computes the flat offseti * cols + jand reads one element from the contiguous buffer — the same arithmetic asreshape-1d-2d'sr * cols + c. - Shape, dtype, and values are shown explicitly here because
ndarray.__repr__output varies with NumPy version and print options.