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)
  1. 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]]grid
  2. coords ← [[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]]coords
  3. cells ← []

    2coords = [[0, 0], [0, 2], [1, 0], [1, 2]]3cells = []4for coord in coords:
    values this step[]cells
  4. coord ← [0, 0]

    3cells = []4for coord in coords:5    i = coord[0]
    values this step[0, 0]coord
  5. i ← 0

    4for coord in coords:5    i = coord[0]6    j = coord[1]
    values this step0i
  6. j ← 0

    5i = coord[0]6j = coord[1]7cells.append(grid[i][j])
    values this step0j
  7. cells ← [1]

    6    j = coord[1]7    cells.append(grid[i][j])8print('RESULT:', cells)
    values this step[] [1]cells
  8. coord ← [0, 2]

    3cells = []4for coord in coords:5    i = coord[0]
    values this step[0, 0] [0, 2]coord
  9. i = coord[0]

    4for coord in coords:5    i = coord[0]6    j = coord[1]
  10. j ← 2

    5i = coord[0]6j = coord[1]7cells.append(grid[i][j])
    values this step0 2j
  11. cells ← [1, 3]

    6    j = coord[1]7    cells.append(grid[i][j])8print('RESULT:', cells)
    values this step[1] [1, 3]cells
  12. coord ← [1, 0]

    3cells = []4for coord in coords:5    i = coord[0]
    values this step[0, 2] [1, 0]coord
  13. i ← 1

    4for coord in coords:5    i = coord[0]6    j = coord[1]
    values this step0 1i
  14. j ← 0

    5i = coord[0]6j = coord[1]7cells.append(grid[i][j])
    values this step2 0j
  15. cells ← [1, 3, 4]

    6    j = coord[1]7    cells.append(grid[i][j])8print('RESULT:', cells)
    values this step[1, 3] [1, 3, 4]cells
  16. coord ← [1, 2]

    3cells = []4for coord in coords:5    i = coord[0]
    values this step[1, 0] [1, 2]coord
  17. i = coord[0]

    4for coord in coords:5    i = coord[0]6    j = coord[1]
  18. j ← 2

    5i = coord[0]6j = coord[1]7cells.append(grid[i][j])
    values this step0 2j
  19. cells ← [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]cells
  20. for coord in coords:

    3cells = []4for coord in coords:5    i = coord[0]
  21. 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 row a[i] (a full Python list), then indexes into it with [j]. NumPy's a[i, j] is a single operation that computes the flat offset i * cols + j and reads one element from the contiguous buffer — the same arithmetic as reshape-1d-2d's r * cols + c.
  • Shape, dtype, and values are shown explicitly here because ndarray.__repr__ output varies with NumPy version and print options.