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