Skip to main content
Albero Academy

Indexing

Access single elements and rows in 1-D and multi-dimensional arrays.

Indexing & Slicing8 min

Getting values out of an array is the day-to-day of analysis. NumPy indexing is richer than a list's, especially in more than one dimension where you index each axis with a comma.

1-D indexing

like a list, plus negatives
a = np.array([10, 20, 30, 40])
a[0] # 10 first
a[-1] # 40 last (negative counts from the end)

Multi-dimensional indexing

For 2-D, use [row, col] — cleaner than the chained [row][col] you'd write for nested lists:

[row, col]
b = np.array([[1, 2, 3],
[4, 5, 6]])
b[0, 2] # 3 row 0, column 2
b[1] # [4 5 6] a whole row
b[1] = 0 # assign a whole row → [[1 2 3] [0 0 0]]

Key takeaways

  1. 01Index from 0; negative indices count from the end.
  2. 02For N-D arrays, index each axis with a comma: b[row, col].
  3. 03A single index on a 2-D array returns a whole row.
  4. 04You can assign through an index to update elements or whole rows.

Finished this chapter?

Mark it complete to track your progress through NumPy.