0
0
NumpyHow-ToBeginner ยท 3 min read

How to Index a NumPy Array: Syntax and Examples

You can index a numpy array using square brackets [] with integers, slices, or boolean arrays. For example, arr[0] accesses the first element, and arr[1:4] accesses a slice from index 1 to 3.
๐Ÿ“

Syntax

Indexing a NumPy array uses square brackets []. Inside the brackets, you can use:

  • Integer index: Access a single element by its position.
  • Slice: Access a range of elements using start:stop:step.
  • Boolean array: Select elements where the condition is True.
  • Multiple indices: Use commas to index multi-dimensional arrays.
python
import numpy as np

arr = np.array([10, 20, 30, 40, 50])

# Integer index
print(arr[0])  # First element

# Slice
print(arr[1:4])  # Elements from index 1 to 3

# Boolean indexing
print(arr[arr > 25])  # Elements greater than 25

# Multi-dimensional indexing
arr2d = np.array([[1, 2], [3, 4]])
print(arr2d[1, 0])  # Element at row 1, column 0
Output
10 [20 30 40] [30 40 50] 3
๐Ÿ’ป

Example

This example shows how to access elements in a 1D and 2D NumPy array using different indexing methods.

python
import numpy as np

# Create a 1D array
arr = np.array([5, 10, 15, 20, 25])

# Access single element
first = arr[0]

# Access slice
slice_part = arr[2:5]

# Boolean indexing
filtered = arr[arr >= 15]

# Create a 2D array
arr2d = np.array([[1, 2, 3], [4, 5, 6]])

# Access element at row 0, column 2
element_2d = arr2d[0, 2]

print("First element:", first)
print("Slice from index 2 to 4:", slice_part)
print("Elements >= 15:", filtered)
print("Element at row 0, col 2:", element_2d)
Output
First element: 5 Slice from index 2 to 4: [15 20 25] Elements >= 15: [15 20 25] Element at row 0, col 2: 3
โš ๏ธ

Common Pitfalls

Common mistakes when indexing NumPy arrays include:

  • Using parentheses () instead of square brackets [].
  • Confusing Python list indexing with NumPy slicing (NumPy slices return views, not copies).
  • Trying to use multiple indices without commas for multi-dimensional arrays.
  • Using out-of-range indices causing IndexError.
python
import numpy as np

arr = np.array([1, 2, 3, 4, 5])

# Wrong: using parentheses
# print(arr(0))  # This raises TypeError

# Correct:
print(arr[0])  # Outputs 1

# Wrong: missing comma in 2D indexing
arr2d = np.array([[1, 2], [3, 4]])
# print(arr2d[1 0])  # SyntaxError

# Correct:
print(arr2d[1, 0])  # Outputs 3
Output
1 3
๐Ÿ“Š

Quick Reference

Indexing TypeSyntax ExampleDescription
Integerarr[2]Access single element at index 2
Slicearr[1:4]Access elements from index 1 to 3
Booleanarr[arr > 10]Access elements where condition is True
Multi-dimensionalarr2d[0, 1]Access element at row 0, column 1
โœ…

Key Takeaways

Use square brackets [] with integers, slices, or boolean arrays to index NumPy arrays.
Multi-dimensional arrays require commas to separate indices for each dimension.
Slices return views, so modifying them changes the original array.
Avoid using parentheses () for indexing; it causes errors.
Check index ranges to prevent IndexError exceptions.