Learning Objectives – after this chapter you will be able to:
  • Access elements of 1-D, 2-D and 3-D arrays by index.
  • Use negative indexes.
  • Select several elements at once with integer-array (fancy) indexing.
  • Select elements by condition with boolean indexing.
Indexing a 1-D Array

Array indexing works like list indexing: the first element is at index 0, and negative indexes count from the end.

import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr[0], arr[3], arr[-1], arr[-2])
print(arr[1] + arr[2])

arr[0] = 99            # arrays are mutable
print(arr)
Output
10 40 50 40
50
[99 20 30 40 50]
Indexing a 2-D Array

Use arr[row, column] (both start at 0). arr[row] alone returns a whole row. arr[r][c] also works, but arr[r, c] is the preferred, faster NumPy style.

import numpy as np

m = np.array([[1, 2, 3, 4],
              [5, 6, 7, 8],
              [9, 10, 11, 12]])
print(m[0, 1])        # row 0, column 1
print(m[2, 3])        # row 2, column 3
print(m[1, -1])       # last column of row 1
print(m[1])           # whole row 1
print(m[-1, -1], m[2][0])
Output
2
12
8
[5 6 7 8]
12 9
Indexing a 3-D Array

Use arr[block, row, column].

import numpy as np

a3 = np.array([[[1, 2, 3], [11, 22, 33]],
               [[4, 5, 6], [44, 55, 66]]])
print(a3[0, 1, 2])     # block 0, row 1, column 2
print(a3[1, 0, 0])     # block 1, row 0, column 0
print(a3[1])           # whole block 1
Output
33
4
[[ 4  5  6]
 [44 55 66]]
Fancy (Integer-Array) Indexing

Pass a list of indexes to pick several elements at once. The result is always a new array (a copy).

import numpy as np

arr = np.array([10, 20, 30, 40, 50])
print(arr[[0, 2, 4]])
print(arr[[4, 4, 1]])          # indexes can repeat

m = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(m[[0, 2]])               # rows 0 and 2
print(m[[0, 1, 2], [2, 1, 0]]) # elements (0,2), (1,1), (2,0)
Output
[10 30 50]
[50 50 20]
[[1 2 3]
 [7 8 9]]
[3 5 7]
Boolean Indexing

A condition on an array produces a boolean array of the same shape. Using it as an index returns only the elements where the value is True (covered in detail in Array Filter).

import numpy as np

scores = np.array([45, 82, 67, 91, 38])
mask = scores >= 60
print(mask)
print(scores[mask])
print(scores[scores < 50])
Output
[False  True  True  True False]
[82 67 91]
[45 38]
Index Out of Range
import numpy as np

arr = np.array([1, 2, 3])
try:
    print(arr[5])
except IndexError as e:
    print("IndexError:", e)
Output
IndexError: index 5 is out of bounds for axis 0 with size 3
Key Takeaways
  • Indexes start at 0; negative indexes count from the end.
  • Use arr[r, c] for 2-D and arr[b, r, c] for 3-D arrays.
  • Lists of indexes (fancy indexing) and boolean masks select many elements and return copies.
Video : Array Indexing
Question / Answer (15 questions)

Answer : Array indexing is accessing an element (or a group of elements) of an array by its position. In NumPy indexes start at 0, and negative indexes count from the end.

Answer : Use the negative index -1, e.g. arr[-1]. For np.array([10, 20, 30, 40, 50]) it returns 50.

Answer : Use arr[row, column], where both indexes start from 0. For m = [[1,2,3,4],[5,6,7,8],[9,10,11,12]], m[2, 3] is 12.

Answer : Both return the same element. m[1, 2] indexes the array once with a tuple; m[1][2] first creates the row m[1] and then indexes it. The comma form is the preferred NumPy style and is more efficient.

Answer : It returns the entire row at index 1 as a 1-D array, e.g. [5 6 7 8].

Answer : Use three indexes: arr[block, row, column]. For a3 = [[[1,2,3],[11,22,33]],[[4,5,6],[44,55,66]]], a3[0, 1, 2] is 33.

Answer : Yes. Arrays are mutable: arr[0] = 99 replaces the first element. The new value is converted to the array’s dtype.

Answer : Fancy indexing passes a list or array of indexes to select several elements at once, e.g. arr[[0, 2, 4]]. The result is a new array (a copy), and indexes may repeat.

Answer : It pairs the row and column lists to select (0,2), (1,1) and (2,0), giving [3 5 7].

Answer : Using a boolean array (usually from a condition such as arr > 50) as an index. Only elements where the mask is True are returned.

Answer : A boolean array of the same shape: [False True True True False]. Using it as scores[scores >= 60] returns [82 67 91].

Answer : IndexError, for example "index 5 is out of bounds for axis 0 with size 3".

Answer : A copy. Changing the result does not change the original array. (Basic slicing, by contrast, returns a view.)

Answer : Pass a list of row indexes: m[[0, 2]].

Answer : Use m[-1, -1].
MCQ Test : Check Your Understanding (5 questions)

Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.

Q1. For arr = np.array([5, 10, 15, 20]), what is arr[-2]?

Show Answer

Correct Answer : B. 15
-1 is 20, so -2 is 15.

Q2. For m = np.array([[1, 2], [3, 4]]), what is m[1, 0]?

Show Answer

Correct Answer : A. 3
Row 1 is [3, 4]; column 0 is 3.

Q3. Which indexing style returns a COPY of the data?

Show Answer

Correct Answer : D. arr[[0, 2]]
Fancy indexing with a list returns a copy; slicing returns a view.

Q4. What does np.array([3, 8, 1])[np.array([3, 8, 1]) > 2] return?

Show Answer

Correct Answer : A. [3 8]
The mask selects elements greater than 2.

Q5. Accessing arr[10] on a 3-element array raises:

Show Answer

Correct Answer : C. IndexError
Out-of-range positions raise IndexError.

Bibliography/References

The notes, questions and MCQs on this page are original explanations written for this course. The works listed below were consulted and are cited for attribution and further reading. Product names, logos and trademarks belong to their respective owners. Short quotations, where used, are marked and attributed.

  1. NumPy Developers. (n.d.). Indexing on ndarrays. NumPy User Guide. https://numpy.org/doc/stable/user/basics.indexing.html
  2. NumPy Developers. (n.d.). NumPy quickstart. NumPy User Guide. https://numpy.org/doc/stable/user/quickstart.html
  3. NumPy Developers. (n.d.). NumPy: the absolute basics for beginners. NumPy User Guide. https://numpy.org/doc/stable/user/absolute_beginners.html
  4. VanderPlas, J. (2016). Python Data Science Handbook. O’Reilly Media. (Free online edition.) https://jakevdp.github.io/PythonDataScienceHandbook/
  5. McKinney, W. (2022). Python for Data Analysis (3rd ed.). O’Reilly Media. (Open-access online edition.) https://wesmckinney.com/book/