# How to subset list elements that lie between two missing values?

you can use the built-in function `zip`

:

```
subset = [e2 for e1, e2, e3 in zip(lst, lst[1:], lst[2:]) if np.isnan(e1) and not np.isnan(e2) and np.isnan(e3)]
print(subset)
```

**output:**

```
[14, 19]
```

I'm a NumPy noob, so probably can be done better...

```
>>> a = np.array(lst)
>>> a[1:-1][np.isnan(a[:-2]) & np.isnan(a[2:])]
array([14., 19.])
```

For my examples `[1, np.nan]`

and `[np.nan]`

in the comments, this produces an empty array as intended.

Or as Georgy commented, do `isnan`

only once:

```
>>> a = np.array(lst)
>>> nan = np.isnan(a)
>>> a[1:-1][nan[:-2] & nan[2:]]
array([14., 19.])
```

As kaya3 commented, if there can be three nans in a row, these solutions would include the middle one in the result (like your original does). Here's one that doesn't (for the test I replaced the 14 with a nan):

```
>>> a[1:-1][nan[:-2] & ~nan[1:-1] & nan[2:]]
array([19.])
```

Use list comprehension

```
import numpy as np
lst=[10,11,12,np.nan, 14, np.nan, 16, 17, np.nan, np.nan, np.nan]
subset = [elem for i, elem in enumerate(lst) if i and i < len(lst)-1 and np.isnan(lst[i-1]) and np.isnan(lst[i+1]) and not np.isnan(elem)]
print(subset)
```

Corrected the mistakes that were pointed out by other contributors. This should work for all the cases now.