HOWTO · NumPy

numpy.where() Multiple Conditions

There are 4 methods that can be used to specify multiple conditions inside the numpy.where() function in Python, the & operator, the | operator, the numpy.logical_and() function, and the numpy.logical_or() function.

On this page

Combine array comparisons with & or |, or use numpy.logical_and() or numpy.logical_or(), then pass the resulting Boolean array to numpy.where(). Parenthesize each comparison when using & or |. The examples below select values greater than 2 and less than 4, or values greater than 2 or divisible by 2. Choose AND when every test must match; choose OR when any test can match. Each comparison produces a Boolean array aligned with values, and combining the arrays creates the condition used for selection.

Each example below prints the selected values; the output follows its code.

Implement numpy.where() Multiple Conditions With the & Operator in Python

Use & to require every comparison to be true for an array element. Parenthesize each comparison because Python’s operator precedence can otherwise change how the expression is evaluated. The following example selects values in the range from 2 to 4, excluding the endpoints:

import numpy as np

values = np.array([1, 2, 3, 4, 5])

result = values[np.where((values > 2) & (values < 4))]
print(result)
[3]

np.where(condition) returns a tuple of matching indices. Indexing values with that tuple retrieves the matching elements, so this example selects the value 3. For more conditions, group each comparison in parentheses and join them with another &; every joined test must be true for the element to remain in the result.

Implement numpy.where() Multiple Conditions With the | Operator in Python

Use | when either comparison can be true. In a NumPy condition, the | operator performs element-wise OR: it combines corresponding Boolean values from the two comparisons. In this example, an element is selected if it is greater than 2 or divisible by 2. Each comparison is parenthesized before the element-wise OR operation:

import numpy as np

values = np.array([1, 2, 3, 4, 5])

result = values[np.where((values > 2) | (values % 2 == 0))]
print(result)
[2 3 4 5]

The value 2 matches the divisibility test, while 3 and 5 match the greater-than test; 4 matches both. numpy.where() returns each matching index once, so 4 appears once in the selected result. You can join more parenthesized comparisons with | when any one of several tests should qualify an element.

Implement numpy.where() Multiple Conditions With the numpy.logical_and() Function

numpy.logical_and() combines two conditions element by element, making it a function form of the & approach. Pass its result to numpy.where() to obtain matching indices, then use those indices to select values:

import numpy as np

values = np.array([1, 2, 3, 4, 5])

condition = np.logical_and(values > 2, values < 4)
result = values[np.where(condition)]
print(result)
[3]

Here both comparisons must be true for an element to be selected. The condition array has the same shape as values for these same-shaped inputs. For additional tests, combine logical-function results in steps, which can make a longer condition easier to read and inspect.

Implement numpy.where() Multiple Conditions With the numpy.logical_or() Function in Python

numpy.logical_or() combines conditions element by element when either one can select an element. As with the operator form, this example selects values greater than 2 or divisible by 2:

import numpy as np

values = np.array([1, 2, 3, 4, 5])

condition = np.logical_or(values > 2, values % 2 == 0)
result = values[np.where(condition)]
print(result)
[2 3 4 5]

Use & and | for element-wise array operations; Python’s scalar and and or do not combine NumPy Boolean arrays. The logical functions are useful when you prefer named operations or need to build a condition in steps. For conditions with compatible shapes, NumPy applies the logical functions element by element. In each example, values[np.where(condition)] uses the index tuple to retrieve the original values; np.where(condition) by itself gives positions, not the values at those positions. Keep the condition arrays shape-compatible with the data you intend to select.