NumPy array indexing selects values by position across one or more axes. It is the normal way to take one element, a row, a column, or a rectangular window from an ndarray before passing that subset into a calculation.
For two-dimensional arrays, the first index addresses rows and the second index addresses columns. A comma separates axes, while a colon keeps all positions on that axis or defines a start, stop, and step range.
Basic integer and slice selections use zero-based positions. Slices stop before the ending index, and basic slices return views when NumPy can point back into the same data buffer; call .copy() before changing a selected block independently.
Related: Filter with a boolean mask
Related: Check view or copy state
Related: Reshape an array
Steps to index and slice a NumPy array:
- Create a script that selects one element, one row, one column, a rectangular window, and a stepped reversed slice.
- array-index-slice.py
import numpy as np readings = np.arange(1, 21).reshape(4, 5) single = readings[2, 3] row = readings[2, :] column = readings[:, 3] window = readings[1:3, 2:5] stepped = readings[::2, ::-1] print("readings:") print(readings) print("single readings[2, 3]:", single) print("row readings[2, :]:", row) print("column readings[:, 3]:", column) print("window readings[1:3, 2:5]:") print(window) print("stepped readings[::2, ::-1]:") print(stepped) print("window shares memory:", np.shares_memory(readings, window)) assert single == 14 assert row.tolist() == [11, 12, 13, 14, 15] assert column.tolist() == [4, 9, 14, 19] assert window.tolist() == [[8, 9, 10], [13, 14, 15]] assert stepped.tolist() == [[5, 4, 3, 2, 1], [15, 14, 13, 12, 11]] assert np.shares_memory(readings, window) print("indexing checks passed")
- Run the script.
$ python3 array-index-slice.py readings: [[ 1 2 3 4 5] [ 6 7 8 9 10] [11 12 13 14 15] [16 17 18 19 20]] single readings[2, 3]: 14 row readings[2, :]: [11 12 13 14 15] column readings[:, 3]: [ 4 9 14 19] window readings[1:3, 2:5]: [[ 8 9 10] [13 14 15]] stepped readings[::2, ::-1]: [[ 5 4 3 2 1] [15 14 13 12 11]] window shares memory: True indexing checks passed
readings[2, 3] selects one scalar. readings[2, :] keeps all columns in row 2, and readings[:, 3] keeps column 3 from every row.
- Check the rectangular slice.
readings[1:3, 2:5] keeps rows 1 and 2, then columns 2 through 4. The stop value is excluded on both axes.
- Check the stepped slice.
readings[::2, ::-1] keeps every second row and reverses each selected row's columns.
- Confirm that the final line shows indexing checks passed.
The assertions prove that the scalar, row, column, window, stepped slice, and memory-sharing checks matched the expected arrays. Use .copy() before editing window when changes should not affect readings.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.