Your expression works if you add parentheses:

>>> y[(1 < x) & (x < 5)]
array(['o', 'o', 'a'], 
      dtype='|S1')
Answer from jfs on Stack Overflow
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.select.html
numpy.select — NumPy v2.1 Manual
numpy.select(condlist, choicelist, default=0)[source]# Return an array drawn from elements in choicelist, depending on conditions. Parameters: condlistlist of bool ndarrays · The list of conditions which determine from which array in choicelist the output elements are taken.
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-select-function-python
numpy.select() function - Python - GeeksforGeeks
July 12, 2025 - The numpy.select() function is used to construct an array by selecting elements from a list of choices based on multiple conditions.
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.select.html
numpy.select — NumPy v2.0 Manual
numpy.select(condlist, choicelist, default=0)[source]# Return an array drawn from elements in choicelist, depending on conditions. Parameters: condlistlist of bool ndarrays · The list of conditions which determine from which array in choicelist the output elements are taken.
🌐
ProjectPro
projectpro.io › recipes › select-elements-from-numpy-array-in-python
How to Select Columns in NumPy Array using np.select? -
February 22, 2024 - NumPy, the fundamental package for scientific computing in Python, provides a powerful tool called np.select for advanced array manipulation.
🌐
NumPy
numpy.org › doc › 1.18 › reference › generated › numpy.select.html
numpy.select — NumPy v1.18 Manual
Return elements from one of two arrays depending on condition. ... >>> x = np.arange(10) >>> condlist = [x<3, x>5] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) array([ 0, 1, 2, ..., 49, 64, 81])
🌐
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.select.html
numpy.select — NumPy v1.25 Manual
Return elements from one of two arrays depending on condition. ... >>> x = np.arange(6) >>> condlist = [x<3, x>3] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist, 42) array([ 0, 1, 2, 42, 16, 25])
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.select.html
numpy.select — NumPy v2.3 Manual
numpy.select(condlist, choicelist, default=0)[source]# Return an array drawn from elements in choicelist, depending on conditions. Parameters: condlistlist of bool ndarrays · The list of conditions which determine from which array in choicelist the output elements are taken.
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Get and set values in an array using various indexing | note.nkmk.me
February 7, 2024 - Using a slice i:i+1 selects a single row or column, preserving the array's dimensions, unlike selection with an integer (int), which reduces the dimensions. NumPy: Get the number of dimensions, shape, and size of ndarray
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.choose.html
numpy.choose — NumPy v2.5 Manual
Given an “index” array (a) of integers and a sequence of n arrays (choices), a and each choice array are first broadcast, as necessary, to arrays of a common shape; calling these Ba and Bchoices[i], i = 0,…,n-1 we have that, necessarily, Ba.shape == Bchoices[i].shape for each i.
🌐
Earth Data Science
earthdatascience.org › home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - Just like for the one-dimensional numpy array, you use the index [1,2] for the second row, third column because Python indexing begins with [0], not with [1] On this page, you will use indexing to select elements within one-dimensional and two-dimensional numpy arrays, a selection process referred to as slicing.
🌐
NumPy
numpy.org › doc › 1.16 › reference › generated › numpy.select.html
numpy.select — NumPy v1.16 Manual
February 18, 2020 - Return elements from one of two arrays depending on condition. ... >>> x = np.arange(10) >>> condlist = [x<3, x>5] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81])
🌐
SciPy
docs.scipy.org › doc › numpy-1.8.1 › reference › generated › numpy.select.html
numpy.select — NumPy v1.8 Manual
Return elements from one of two arrays depending on condition. ... >>> x = np.arange(10) >>> condlist = [x<3, x>5] >>> choicelist = [x, x**2] >>> np.select(condlist, choicelist) array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81])
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.choose.html
numpy.choose — NumPy v2.1 Manual
Construct an array from an index array and a list of arrays to choose from. First of all, if confused or uncertain, definitely look at the Examples - in its full generality, this function is less simple than it might seem from the following code description (below ndi = numpy.lib.index_tricks):
🌐
Medium
satnamsingh99.medium.com › np-select-vs-np-where-how-to-choose-the-right-function-for-your-numpy-arrays-a0093a96b1d9
np.select vs. np.where: How to Choose the Right Function for Your NumPy Arrays | by Satnam Singh | Medium
February 20, 2023 - In this example, the np.select function is used to select elements from two arrays, arr1 and arr2, based on two conditions. The function evaluates the first condition on arr1 and selects the values from arr1 if the condition is true.
Top answer
1 of 4
151

As Toan suggests, a simple hack would be to just select the rows first, and then select the columns over that.

>>> a[[0,1,3], :]            # Returns the rows you want
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [12, 13, 14, 15]])
>>> a[[0,1,3], :][:, [0,2]]  # Selects the columns you want as well
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

[Edit] The built-in method: np.ix_

I recently discovered that numpy gives you an in-built one-liner to doing exactly what @Jaime suggested, but without having to use broadcasting syntax (which suffers from lack of readability). From the docs:

Using ix_ one can quickly construct index arrays that will index the cross product. a[np.ix_([1,3],[2,5])] returns the array [[a[1,2] a[1,5]], [a[3,2] a[3,5]]].

So you use it like this:

>>> a = np.arange(20).reshape((5,4))
>>> a[np.ix_([0,1,3], [0,2])]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

And the way it works is that it takes care of aligning arrays the way Jaime suggested, so that broadcasting happens properly:

>>> np.ix_([0,1,3], [0,2])
(array([[0],
        [1],
        [3]]), array([[0, 2]]))

Also, as MikeC says in a comment, np.ix_ has the advantage of returning a view, which my first (pre-edit) answer did not. This means you can now assign to the indexed array:

>>> a[np.ix_([0,1,3], [0,2])] = -1
>>> a    
array([[-1,  1, -1,  3],
       [-1,  5, -1,  7],
       [ 8,  9, 10, 11],
       [-1, 13, -1, 15],
       [16, 17, 18, 19]])
2 of 4
102

Fancy indexing requires you to provide all indices for each dimension. You are providing 3 indices for the first one, and only 2 for the second one, hence the error. You want to do something like this:

>>> a[[[0, 0], [1, 1], [3, 3]], [[0,2], [0,2], [0, 2]]]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

That is of course a pain to write, so you can let broadcasting help you:

>>> a[[[0], [1], [3]], [0, 2]]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])

This is much simpler to do if you index with arrays, not lists:

>>> row_idx = np.array([0, 1, 3])
>>> col_idx = np.array([0, 2])
>>> a[row_idx[:, None], col_idx]
array([[ 0,  2],
       [ 4,  6],
       [12, 14]])
🌐
Medium
medium.com › @amit25173 › how-numpy-select-works-f11ed0de629b
How numpy.select() Works?. If you think you need to spend $2,000… | by Amit Yadav | Medium
February 8, 2025 - Step 1: We define an array arr with values [1, 2, 3, 4, 5]. Step 2: We set up two conditions: values in arr that are less than 3 (arr < 3) and values that are greater than or equal to 3 (arr >= 3). Step 3: Then, we specify what we want to do ...
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.choose.html
numpy.choose — NumPy v2.6.dev0 Manual
Given an “index” array (a) of integers and a sequence of n arrays (choices), a and each choice array are first broadcast, as necessary, to arrays of a common shape; calling these Ba and Bchoices[i], i = 0,…,n-1 we have that, necessarily, Ba.shape == Bchoices[i].shape for each i.