The best way in your particular case would just be to change your two criteria to one criterion:

dists[abs(dists - r - dr/2.) <= dr/2.]

It only creates one boolean array, and in my opinion is easier to read because it says, is dist within a dr or r? (Though I'd redefine r to be the center of your region of interest instead of the beginning, so r = r + dr/2.) But that doesn't answer your question.


The answer to your question:
You don't actually need where if you're just trying to filter out the elements of dists that don't fit your criteria:

dists[(dists >= r) & (dists <= r+dr)]

Because the & will give you an elementwise and (the parentheses are necessary).

Or, if you do want to use where for some reason, you can do:

 dists[(np.where((dists >= r) & (dists <= r + dr)))]

Why:
The reason it doesn't work is because np.where returns a list of indices, not a boolean array. You're trying to get and between two lists of numbers, which of course doesn't have the True/False values that you expect. If a and b are both True values, then a and b returns b. So saying something like [0,1,2] and [2,3,4] will just give you [2,3,4]. Here it is in action:

In [230]: dists = np.arange(0,10,.5)
In [231]: r = 5
In [232]: dr = 1

In [233]: np.where(dists >= r)
Out[233]: (array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),)

In [234]: np.where(dists <= r+dr)
Out[234]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

In [235]: np.where(dists >= r) and np.where(dists <= r+dr)
Out[235]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

What you were expecting to compare was simply the boolean array, for example

In [236]: dists >= r
Out[236]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True], dtype=bool)

In [237]: dists <= r + dr
Out[237]: 
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

In [238]: (dists >= r) & (dists <= r + dr)
Out[238]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

Now you can call np.where on the combined boolean array:

In [239]: np.where((dists >= r) & (dists <= r + dr))
Out[239]: (array([10, 11, 12]),)

In [240]: dists[np.where((dists >= r) & (dists <= r + dr))]
Out[240]: array([ 5. ,  5.5,  6. ])

Or simply index the original array with the boolean array using fancy indexing

In [241]: dists[(dists >= r) & (dists <= r + dr)]
Out[241]: array([ 5. ,  5.5,  6. ])
Answer from askewchan on Stack Overflow
Top answer
1 of 2
316

The best way in your particular case would just be to change your two criteria to one criterion:

dists[abs(dists - r - dr/2.) <= dr/2.]

It only creates one boolean array, and in my opinion is easier to read because it says, is dist within a dr or r? (Though I'd redefine r to be the center of your region of interest instead of the beginning, so r = r + dr/2.) But that doesn't answer your question.


The answer to your question:
You don't actually need where if you're just trying to filter out the elements of dists that don't fit your criteria:

dists[(dists >= r) & (dists <= r+dr)]

Because the & will give you an elementwise and (the parentheses are necessary).

Or, if you do want to use where for some reason, you can do:

 dists[(np.where((dists >= r) & (dists <= r + dr)))]

Why:
The reason it doesn't work is because np.where returns a list of indices, not a boolean array. You're trying to get and between two lists of numbers, which of course doesn't have the True/False values that you expect. If a and b are both True values, then a and b returns b. So saying something like [0,1,2] and [2,3,4] will just give you [2,3,4]. Here it is in action:

In [230]: dists = np.arange(0,10,.5)
In [231]: r = 5
In [232]: dr = 1

In [233]: np.where(dists >= r)
Out[233]: (array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),)

In [234]: np.where(dists <= r+dr)
Out[234]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

In [235]: np.where(dists >= r) and np.where(dists <= r+dr)
Out[235]: (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]),)

What you were expecting to compare was simply the boolean array, for example

In [236]: dists >= r
Out[236]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True], dtype=bool)

In [237]: dists <= r + dr
Out[237]: 
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

In [238]: (dists >= r) & (dists <= r + dr)
Out[238]: 
array([False, False, False, False, False, False, False, False, False,
       False,  True,  True,  True, False, False, False, False, False,
       False, False], dtype=bool)

Now you can call np.where on the combined boolean array:

In [239]: np.where((dists >= r) & (dists <= r + dr))
Out[239]: (array([10, 11, 12]),)

In [240]: dists[np.where((dists >= r) & (dists <= r + dr))]
Out[240]: array([ 5. ,  5.5,  6. ])

Or simply index the original array with the boolean array using fancy indexing

In [241]: dists[(dists >= r) & (dists <= r + dr)]
Out[241]: array([ 5. ,  5.5,  6. ])
2 of 2
96

The accepted answer explained the problem well enough. However, the more Numpythonic approach for applying multiple conditions is to use numpy logical functions. In this case, you can use np.logical_and:

np.where(np.logical_and(np.greater_equal(dists,r),np.greater_equal(dists,r + dr)))
🌐
Medium
medium.com › @whyamit101 › using-numpy-where-with-single-and-multiple-conditions-dfc668042dd6
Using numpy.where with Single and Multiple Conditions | by why amit | Medium
February 9, 2025 - numpy.where checks which elements satisfy the condition. It returns the indices of these elements. Using these indices, you can retrieve the actual elements from your array. It’s like creating a map to find buried treasure in your data.
Discussions

python - np.where multiple return values - Stack Overflow
Using pandas and numpy I am trying to process a column in a dataframe, and want to create a new column with values relating to it. So if in column x the value 1 is present, in the new column it would be a, for value 2 it would be b etc ... And I can find example of multiple conditions i.e if ... More on stackoverflow.com
🌐 stackoverflow.com
python - Numpy "where" with multiple conditions - Stack Overflow
I try to add a new column "energy_class" to a dataframe "df_energy" which contains the string "high" if the "consumption_energy" value > 400, "medium... More on stackoverflow.com
🌐 stackoverflow.com
python - How to give multiple conditions to numpy.where() - Stack Overflow
I have a numpy array like this: letters = np.array([A, B, C, A, B, C, A, B, C]) I'm trying to return another array containing all the indexes of certain items in the array above. I have tried: More on stackoverflow.com
🌐 stackoverflow.com
What is the correct way to apply np.select() on one row at a time in numpy and pandas?
In general, if you find yourself using iterrows, stop. It's the worst performing option of all the iteration options. I'd love to help, but this code is extremely hard to read without formatting. The flow here doesn't make a lot of sense to me... there's an iterrows loop, but then series operations? Either do everything on a row by row basis, or numpy series basis... but not both. Could you reformat it with markdown (triple ticks) and indents so it's readable? Also, because I prefer SQL for this kind of logic, I'd write it in your favorite db engine (mine is duckdb) import duckdb scored_df = duckdb.execute(""" select case when retailer_id in (select retailer_id from droppers) then -1 when months_sr > 0.4 or (historical_sr > 0.4 and orders_this_month_total >= 1 then 0 when wallet_amount > 0 and orders_this_month_total > 0 then 1 when orders_this_month_total =1 then 2 end as score, df.* from df where target='balanced' union all select case when retailer_id in (select retailer_id from droppers) then -1 when months_sr > 0.4 or (historical_sr > 0.4 and orders_this_month_total >= 1 then 0 when wallet_amount > 0 and orders_this_month_total > 0 then 1 when orders_this_month_total =1 then 2 end as score, df.* from df where target='nmv' """).df() More on reddit.com
🌐 r/pythontips
7
5
May 26, 2023
🌐
Real Python
realpython.com › numpy-where-conditional-expressions
How to Use Conditional Expressions With NumPy where() – Real Python
July 12, 2026 - The NumPy where() function is a powerful tool for filtering array elements in lists, tuples, and NumPy arrays. It works by using a conditional predicate, similar to the logic used in the WHERE or HAVING clauses in SQL queries.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-use-numpy-where-with-multiple-conditions-in-python
How to use NumPy where() with multiple conditions in Python ? - GeeksforGeeks
July 23, 2025 - In the above syntax, we can see the where() function can take two arguments in which one is mandatory and another one is optional. If the value of the condition is true an array will be created based on the indices.
🌐
Medium
medium.com › @heyamit10 › understanding-numpy-where-with-two-conditions-21871ed01aa6
Understanding numpy.where() with Two Conditions | by Hey Amit | Medium
March 6, 2025 - Example 2: Replacing Values Based on Two Conditions · Now, let’s say you want to replace certain values entirely. Here’s the task: Replace all values that are between 15 and 25 (inclusive) with 0. ... import numpy as np # Step 1: Create a NumPy array arr = np.array([10, 15, 20, 25, 30]) # Step 2: Apply two conditions and replace matching values with 0 arr = np.where((arr >= 15) & (arr <= 25), 0, arr) # Step 3: Display the modified array print(arr)
Find elsewhere
🌐
AlgoCademy
algocademy.com › blog › mastering-conditional-operations-in-python-a-comprehensive-guide-to-np-where
Mastering Conditional Operations in Python: A Comprehensive Guide to np.where – AlgoCademy Blog
October 12, 2024 - np.where is a function in Python’s ... true, and another value for when it’s false. Yes, you can use np.where with multiple conditions by chaining it or using logical operators....
🌐
IncludeHelp
includehelp.com › python › numpy-where-function-multiple-conditions.aspx
Python - NumPy 'where' function multiple conditions
To tackle the problem of comparing two conditions only, we check the value with np.where() condition to check all the three conditions and assign the values to them. ... # Importing pandas package import pandas as pd # Import numpy package import numpy as np # Creating a Dictionary d = ...
🌐
Delft Stack
delftstack.com › home › howto › numpy › python numpy.where multiple conditions
numpy.where() Multiple Conditions | Delft Stack
January 30, 2023 - We can specify multiple conditions inside the numpy.where() function by enclosing each condition inside a pair of parenthesis and using a | operator between them.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
numpy.where(): Manipulate elements depending on conditions | note.nkmk.me
November 5, 2019 - How to fix "ValueError: The truth value ... is ambiguous" in NumPy, pandas · Even in the case of multiple conditions, it is not necessary to use np.where() to get the boolean ndarray.
🌐
Statology
statology.org › home › how to use numpy where() with multiple conditions
How to Use NumPy where() With Multiple Conditions
November 9, 2021 - import numpy as np #define NumPy array of values x = np.array([1, 3, 3, 6, 7, 9, 12, 13, 15, 18, 20, 22]) #select values that meet two conditions x[np.where((x > 5) & (x < 20))] array([6, 7, 9, 12, 13, 15, 18])
🌐
pythoncodelab
pythoncodelab.com › home › numpy where with multiple conditions
NumPy where function with multiple conditions - pythoncodelab.com
November 2, 2024 - Value x is returned when the condition becomes true and similarly y is returned when condition is false. For multiple conditions, combine them using logical operators within the condition.
🌐
Cherry Servers
cherryservers.com › home › blog › cloud computing › how to use numpy.where function [with examples]
How to Use numpy.where Function [With Examples]
November 7, 2025 - The new array will contain elements from the x array where the condition x > 2 is true and elements from the y array where the condition is false. ... When passing all the 3 arguments to a numpy.where() function, all the arrays MUST be of the same length.
🌐
IncludeHelp
includehelp.com › python › numpy-where-method-with-multiple-conditions.aspx
Python | NumPy where() Method with Multiple Conditions
To use `numpy.where()` with multiple conditions, just put the conditional expression having multiple conditions combined with logical OR (|) and logical AND (&) operators inside the `where()` method.
🌐
sebhastian
sebhastian.com › numpy-where-multiple-conditions
NumPy where() multiple conditions | sebhastian
May 15, 2023 - To use where() to filter an array with multiple conditions, you can use the and (&) or or (|) operator. You need to put each condition separately inside parentheses. For example, suppose you want to filter the array to get elements that are ...
🌐
Machine Learning Plus
machinelearningplus.com › blog › how to use numpy where function?
How to Use Numpy Where Function? - machinelearningplus
January 26, 2023 - These conditions can vary. ... The numpy.where() function is used to filter data based on the conditions provided. These conditions can vary from being as simple as value comparisons to nested bit-wise conditions.
🌐
CASRAI
casrai.org › guides › research tools & software › statistical software for research › numpy where: syntax, multiple conditions and np.select
NumPy where: Syntax and Multiple Conditions — CASRAI
1 week ago - NumPy where explained: np.where(condition, x, y), multiple conditions with & and |, np.select for 3+ outcomes, and the nonzero form, with outputs.
🌐
CodeSpeedy
codespeedy.com › home › numpy where() with multiple conditions in python
NumPy where() with multiple conditions in Python - CodeSpeedy
February 21, 2022 - numpy.where(condition[, x, y]) Where x and y are two arrays. When the condition is true then the element in x must be considered and when the condition is false then the element in y must be considered.