30 Mins

5. Basic Indexing and Slicing

1D and Multi-Dimensional Array Indexing

In NumPy, indexing allows you to access specific elements within an array. For 1D arrays, the index starts at 0, similar to standard Python lists.

For multi-dimensional arrays, you use a comma-separated tuple of integers to specify the coordinate. For instance, in a 2D array, array[row, column] accesses the element at the intersection of the specified row and column.

Accessing elements via indices provides O(1) performance, making it the most efficient way to pinpoint data.

Operator Note: Always remember that NumPy indices are zero-based, meaning the first element is always index 0.
Python 3
import numpy as np

arr_1d = np.array([10, 20, 30])
print(arr_1d[1]) # Accessing 1D index

arr_2d = np.array([[1, 2], [3, 4]])
print(arr_2d[1, 0]) # Accessing 2D coordinate (row 1, col 0)

Slice Notation Rules (start:stop:step)

Slicing allows you to extract a subset of an array using the colon syntax: [start:stop:step].

The start index is inclusive, the stop index is exclusive, and step defines the increment between selected elements. If step is omitted, it defaults to 1.

This notation works across all dimensions, where you can define a slice for every axis by separating them with commas.

Operator Note: If you leave the start or stop fields empty, NumPy defaults to the beginning or the end of the dimension respectively.
Python 3
import numpy as np

arr = np.array([0, 1, 2, 3, 4, 5])
# Slice from index 1 to 4 with step 2
subset = arr[1:5:2]
print(subset)

Negative Indexing and Stepping Backwards

Negative indices allow you to reference elements from the end of an array. -1 corresponds to the last element, -2 to the second to last, and so on.

When using a negative step value, such as -1, you can effectively reverse an array or iterate through it in descending order.

This is particularly useful when you need to access data relative to the end of a dataset without knowing the total array length.

Operator Note: Combining negative indexing with slicing is a powerful pattern for data cleaning and time-series analysis.
Python 3
import numpy as np

arr = np.array([10, 20, 30, 40])
# Reverse the array using negative step
reversed_arr = arr[::-1]
print(reversed_arr)

Updating Array Slices and Scalar Assignment

NumPy allows you to update slices of an array directly. This is called broadcasting an assignment.

If you assign a single scalar value to a slice, every element within that slice will be updated to the specified value. This is highly efficient as it operates in-place on the memory buffer.

Be aware that slicing returns a view of the original array, meaning changes to a slice modify the original data.

Operator Note: Because slices are views, use .copy() if you want to modify a subset without changing the original array.
Python 3
import numpy as np

arr = np.zeros((3, 3))
arr[0, :] = 5 # Set the entire first row to 5
print(arr)

Knowledge Check

Ready to test your understanding of 5. Basic Indexing and Slicing?

Back