Skip to content
Justice Tech School

Boolean Indexing

A comparison creates a boolean mask. Use it to select elements. Combine conditions with & and | in parentheses.

Example
import numpy as np
a = np.array([5, 12, 7, 20])
mask = a > 6
print(mask)
print(a[mask])
print(a[(a > 6) & (a < 15)])

Result

[False  True  True  True]
[12  7 20]
[12  7]

Exercise

Select values above 6.

a[a  6]

Quiz Test

Which operator combines masks?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.