# Subsetting an np array

**URL:** <https://discuss.python.org/t/subsetting-an-np-array/23339>\
**Category:** Python Help\
**Created:** [January 30, 2023, 10:11pm UTC](https://discuss.python.org/t/subsetting-an-np-array/23339 "2023-01-30T22:11:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![john316](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@john316](https://discuss.python.org/u/john316)\
**Post date:** [January 30, 2023, 10:11pm UTC](https://discuss.python.org/t/subsetting-an-np-array/23339/1 "2023-01-30T22:11:50Z")

</div>

Two questions on subsetting an np array:  
(1)

```auto
a = np.array([-1, 0.25, 0.5, 1, 2, 5, 10, 20, 30])
a[(a > 1) & (a <20)]
>>array([2., 5., 10.])

```

Is there any shorter way to write? This does not work:

```auto
a[1< a < 20]

```

(2)  
I want to store the indices for future use. Is there any better ways? I got an warning from here.

```auto
id1 = [(a > 1) & (a <20)]
a[id1]

/var/folders/b4/db1qr8cn127_gkbtlsjb35w00000gn/T/ipykernel_17312/3816891839.py:1: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  a[id1]
Out[143]:
array([2., 5., 10.])

```

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [January 31, 2023, 12:44am UTC](https://discuss.python.org/t/subsetting-an-np-array/23339/2 "2023-01-31T00:44:39Z")

</div>

(1) The reason why `1 < a < 20` doesn’t work for numpy is that `1 < a < 20` in Python is basically short for `(1 < a) and (a < 20)`, but `1 < a` and `a < 20` both return arrays and Python’s `and` wants to know whether its arguments are true or false, whereas numpy would like it to work elementwise. The workaround for `and` is to use `&`, but then you can’t chain the comparisons.

(2) Drop the `[...]`, which are putting the array in a list. Write `id1 = (a > 1) & (a <20)` instead.

---

<div class="post-metadata">

**Author:** ![PythonCHB](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pythonchb/32/2551_2.png) [@PythonCHB](https://discuss.python.org/u/PythonCHB)\
**Post date:** [January 31, 2023, 5:11am UTC](https://discuss.python.org/t/subsetting-an-np-array/23339/3 "2023-01-31T05:11:10Z")

</div>

`id1 = (a > 1) & (a <20)`

will give you a boolean array, not the indexes – often refered to as a mask – but it will work as you want it to.

NOTE: this is standard Python – `id1 = (a > 1) & (a <20)` is an expression – when evaluated, it returns a value. In this case, that value can be used to index a numpy array, so:

```auto
mask = id1 = (a > 1) & (a <20)
a[mask]

```

is the same as:

```auto
a[id1 = (a > 1) & (a <20)]
```
