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)))
๐ŸŒ
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.
People also ask

How does Python numpy where work?
After import numpy as np , np.where(condition, x, y) returns an array with elements from x where the condition is True and from y elsewhere. With only condition , it returns the indices of True elements as a tuple of arrays.
๐ŸŒ
casrai.org
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
How do I use numpy where with multiple conditions?
Combine boolean arrays with &amp; (and) or | (or), wrapping each comparison in parentheses, for example np.where((v &gt; 1) &amp; (v &lt; 4), 1, 0) . For three or more distinct outcomes, use np.select , where the first matching condition wins.
๐ŸŒ
casrai.org
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
Should I use np.where or np.nonzero to find indices?
The NumPy manual says that using nonzero directly should be preferred, because it behaves correctly for subclasses.
๐ŸŒ
casrai.org
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
๐ŸŒ
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)
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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-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.
๐ŸŒ
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 = ...
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-use-numpy-where-with-multiple-conditions-in-python
Numpy where() Function
October 13, 2023 - In the following example, we have ... print("Array after replacement:", result) ... We can also use multiple conditions with where() function....
๐ŸŒ
AskPython
askpython.com โ€บ python-modules โ€บ numpy โ€บ python-numpy-where
np.where in Python: Find Indices and Replace Values with numpy.where() - AskPython
September 29, 2026 - Call np.where with only a condition and it answers with a tuple of index positions, not the values themselves. That shape catches you on the first line you
๐ŸŒ
Real Python
realpython.com โ€บ numpy-where-conditional-expressions
How to Use Conditional Expressions With NumPy where() โ€“ Real Python
July 12, 2026 - While the where() function can cope with this as its condition parameter, using it with and raises the error. The solution is to use the bitwise AND operator (&) instead. In NumPy, this operator is overloaded to do elementwise AND operations.
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ numpy-where-function-multiple-conditions.html
Numpy where function multiple conditions
You can use NumPy's np.where() function to apply multiple conditions by combining them with logical operators like & (for "and") and | (for "or").