How to Filter a NumPy Array Using a Boolean Index List


In this example, we will learn how to filter a NumPy array using a boolean index list in Python.

Source Code

import numpy as np

a = np.array([5, 6, 7, 8, 9, 10, 11, 12])
b = [True, False, True, False, True, False, True, False]

c = a[b]
print(c)

Output:

[ 5  7  9 11]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement