Question: I have a numpy boolean `selector` array which I can apply to array `a`. *(not actually random in the problem domain, this is just convenient for the example)*. But I actually want to select using only the first n True entries of `selector` *(up to n=3 in the example)*. So given `selector` plus a parameter `n`, how do I generate `select_first_few`, using numpy operations, thus avoiding an iterative loop?

```
>>> import numpy as np
>>> selector = np.random.random(10) > 0.5
>>> a = np.arange(10)
>>> selector
array([ True, False,  True,  True,  True, False,  True, False,  True,
       False])
>>> chosen, others = a[selector], a[~selector]
>>> chosen
array([0, 2, 3, 4, 6, 8])
>>> others
array([1, 5, 7, 9])
>>> select_first_few = np.array([ True, False,  True,  True,  False, False,  False, False,  False,
...        False])
>>> chosen_few, tough_luck = a[select_first_few], a[~select_first_few]
>>> chosen_few
array([0, 2, 3])
>>> tough_luck
array([1, 4, 5, 6, 7, 8, 9])

```

Answer: 
**Approach #1**

One approach would be using cumsum and argmax to get the extent and then slice thereafter to set `False` -

```
In [40]: n = 3

In [41]: selector
Out[41]: 
array([ True, False,  True,  True,  True, False,  True, False,  True,
       False])

In [42]: selector[(selector.cumsum()>n).argmax():] = 0

In [43]: selector # your select_first_few mask
Out[43]: 
array([ True, False,  True,  True, False, False, False, False, False,
       False])

```

Then, use this new `selector` to select and de-select elements off the input array.

**Approach #2**

Another approach would be to *mask-the-mask* -

```
n = 3
C = np.count_nonzero(selector)
newmask = np.zeros(C, dtype=bool)
newmask[:n] = 1
selector[selector] = newmask

```

Sample run -

```
In [62]: selector
Out[62]: 
array([ True, False,  True,  True,  True, False,  True, False,  True,
       False])

In [63]: n = 3
    ...: C = np.count_nonzero(selector)
    ...: newmask = np.zeros(C, dtype=bool)
    ...: newmask[:n] = 1
    ...: selector[selector] = newmask

In [64]: selector
Out[64]: 
array([ True, False,  True,  True, False, False, False, False, False,
       False])

```

Or make it shorter with on-the-fly concatenation of booleans -

```
n = 3
C = np.count_nonzero(selector)
selector[selector] = np.r_[np.ones(n,dtype=bool),np.zeros(C-n,dtype=bool)]

```

**Approach #3**

Most simplistic one -

```
selector &= selector.cumsum()<=n

```
Get the all the choosen indices in a list and slice this list.
Then use list comprehension to retrieve the data at those choosen indices.

```
import numpy as np
selector = np.random.random(10) > 0.5
data = np.arange(10)

choosen_indices = np.where(selector)

#select first 3 choosen
choosen_few_indices = choosen_indices[:3]
choosen_few = [data[i] for i in choosen_few_indices]

# if you are also interested in the not choosen data
not_choosen_indices = list(set(range(len(data))) - set(choosen_indices))
# proceed ...

```