Row 0 and column 1 extracted from a 2×3 grid. The replay shows row assigned directly from grid[r], then a loop building col by pulling one element per row at the fixed column index c.

By hand

With NumPy

a[r, :] selects all columns of row r (the : means "every index along this axis"). a[:, c] selects all rows of column c. The snapshot shows the full array then each extraction labeled by its slice expression.

naive.py
grid = [[1, 2, 3], [4, 5, 6]]
r = 0
c = 1
row = grid[r]
col = []
for i in range(len(grid)):
    col.append(grid[i][c])
print('RESULT:', (row, col))
library.py
import numpy as np

grid = [[1, 2, 3], [4, 5, 6]]
a = np.array(grid)
r, c = 0, 1
row = a[r, :]
col = a[:, c]
print('shape:', a.shape)
print('dtype:', a.dtype)
print('values:', a.tolist())
print(f'a[{r}, :]: shape: {row.shape} dtype: {row.dtype} values: {row.tolist()}')
print(f'a[:, {c}]: shape: {col.shape} dtype: {col.dtype} values: {col.tolist()}')
print('RESULT:', (row.tolist(), col.tolist()))
shape: (2, 3)
dtype: int64
values: [[1, 2, 3], [4, 5, 6]]
a[0, :]: shape: (3,) dtype: int64 values: [1, 2, 3]
a[:, 1]: shape: (2,) dtype: int64 values: [2, 5]
RESULT: ([1, 2, 3], [2, 5])

Implementation notes

  • Axis 0 runs along rows (top to bottom); axis 1 runs along columns (left to right). a[r, :] holds axis 0 fixed and ranges over axis 1; a[:, c] is the reverse.
  • Both a[r, :] and a[:, c] return views, not copies — the same rule as basic slicing. A column extracted this way shares memory with the original array.
  • Shape, dtype, and values are shown explicitly here because ndarray.__repr__ output varies with NumPy version and print options.