Skip to content
scipython.com

Q6.1.6Boolean indexing

Write an expression, using boolean indexing, which returns only the values from an array that have magnitudes between 0 and 1.

Show solution

For example,

In [a]: a = np.array([0, -1, 4.5, 0.5, -0.2, 1.1])
In [x]: a[abs(a)<=1]
Out[x]: array([ 0. , -1. ,  0.5, -0.2])