How do they achieve internally that you are able to pass something like x > 5 into a method?

The short answer is that they don't.

Any sort of logical operation on a numpy array returns a boolean array. (i.e. __gt__, __lt__, etc all return boolean arrays where the given condition is true).

E.g.

x = np.arange(9).reshape(3,3)
print x > 5

yields:

array([[False, False, False],
       [False, False, False],
       [ True,  True,  True]], dtype=bool)

This is the same reason why something like if x > 5: raises a ValueError if x is a numpy array. It's an array of True/False values, not a single value.

Furthermore, numpy arrays can be indexed by boolean arrays. E.g. x[x>5] yields [6 7 8], in this case.

Honestly, it's fairly rare that you actually need numpy.where but it just returns the indicies where a boolean array is True. Usually you can do what you need with simple boolean indexing.

Answer from Joe Kington on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-where-in-python
numpy.where() in Python - GeeksforGeeks
September 30, 2025 - Interview Prep · DSA · Practice ... · DevOps · Last Updated : 30 Sep, 2025 · numpy.where() is used for conditional selection and replacement in NumPy arrays....
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
Discussions

How does python numpy.where() work? - Stack Overflow
There can also be overhead in some cases using the __getitem__ syntax of [] over either numpy.where or numpy.take. Since __getitem__ has to also support slicing, there's some overhead. I've seen noticeable speed differences when working with the Python Pandas data structures and logically indexing ... More on stackoverflow.com
🌐 stackoverflow.com
python - How do I use numpy.where()? What should I pass, and what does the result mean? - Stack Overflow
I found How does python numpy.where() work? but it didn't answer my question because it seems to be about the implementation rather than about how to use it. Numpy where() on a 2D matrix also didn't explain things for me; I'm looking for a step-by-step explanation, rather than a how-to guide for a specific case. Please include ... More on stackoverflow.com
🌐 stackoverflow.com
passing function to numpy.where
using np.where in a dataframe Why not just use df.apply with a ternary operator for this? The general outline would be df.apply(lambda x: function(x) if condition else 0) More on reddit.com
🌐 r/learnpython
6
2
February 17, 2022
Numpy array is the worst part of python.
I am not the biggest fan of weakly typed languages and python is slow, but it defiantly has a place and it does what it's designed to do pretty well. However, the way numpy organises its array syntax and the general working of array is probably the most confusing poorly designed thing in the entire ... More on reddit.com
🌐 r/Numpy
16
22
May 10, 2020
People also ask

Why does np.where return a tuple?
Each array in the tuple describes one input dimension, so a two-dimensional condition produces separate row and column arrays with corresponding entries. A one-dimensional condition still returns a tuple, which is why its printed result contains a trailing comma after the index array.
🌐
askpython.com
askpython.com › python-modules › numpy › python-numpy-where
np.where in Python: Find Indices and Replace Values with ...
What does np.where return with one argument?
It returns a tuple of index arrays identifying the positions where the condition is true. To retrieve the matching values, use that tuple to index the original array, or use boolean indexing directly when you don't need the positions separately.
🌐
askpython.com
askpython.com › python-modules › numpy › python-numpy-where
np.where in Python: Find Indices and Replace Values with ...
Does np.where modify the original array?
The three-argument form returns a new array rather than assigning values into the input, so store its return value to use the replacement. If you intend to edit an existing array, boolean-index assignment makes that mutation explicit and follows the target array's dtype constraints.
🌐
askpython.com
askpython.com › python-modules › numpy › python-numpy-where
np.where in Python: Find Indices and Replace Values with ...
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.where.html
numpy.where — NumPy v2.6.dev0 Manual
Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.where.html
numpy.where — NumPy v2.2 Manual
[xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
🌐
IONOS
ionos.com › digital guide › websites › web development › python np.where method
What is np.where in Python? - IONOS
February 1, 2025 - The Python function np.where() is a powerful method from the NumPy library and is used for selecting elements from an array. It iden­ti­fies and extracts elements that meet a certain condition and then returns indices or values that cor­re­spond ...
🌐
DataCamp
datacamp.com › doc › numpy › where
NumPy where()
The `where()` function in NumPy is used for array computation and analysis; it returns elements chosen from `x` or `y` depending on `condition`, or the indices of elements that meet the condition when only `condition` is provided.
Find elsewhere
🌐
Programiz
programiz.com › python-programming › numpy › methods › where
NumPy where() | Programiz
August 30, 2023 - The NumPy where() method finds indices that are true in an array based on a given condition. The numpy.where() method returns a new array based on a condition applied to each element of an array.
🌐
Machine Learning Plus
machinelearningplus.com › blog › how to use numpy where function?
How to Use Numpy Where Function? - machinelearningplus
January 26, 2023 - The numpy.where() function takes in the condition as one of the required arguments and returns the indices array for elements which satisfy the given condition. Condition is nothing but an expression involving usage of operators with the input array.
🌐
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
Top answer
1 of 4
78

How do they achieve internally that you are able to pass something like x > 5 into a method?

The short answer is that they don't.

Any sort of logical operation on a numpy array returns a boolean array. (i.e. __gt__, __lt__, etc all return boolean arrays where the given condition is true).

E.g.

x = np.arange(9).reshape(3,3)
print x > 5

yields:

array([[False, False, False],
       [False, False, False],
       [ True,  True,  True]], dtype=bool)

This is the same reason why something like if x > 5: raises a ValueError if x is a numpy array. It's an array of True/False values, not a single value.

Furthermore, numpy arrays can be indexed by boolean arrays. E.g. x[x>5] yields [6 7 8], in this case.

Honestly, it's fairly rare that you actually need numpy.where but it just returns the indicies where a boolean array is True. Usually you can do what you need with simple boolean indexing.

2 of 4
25

Old Answer it is kind of confusing. It gives you the LOCATIONS (all of them) of where your statment is true.

so:

>>> a = np.arange(100)
>>> np.where(a > 30)
(array([31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
       48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
       65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
       82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
       99]),)
>>> np.where(a == 90)
(array([90]),)

a = a*40
>>> np.where(a > 1000)
(array([26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
       43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
       60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
       77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
       94, 95, 96, 97, 98, 99]),)
>>> a[25]
1000
>>> a[26]
1040

I use it as an alternative to list.index(), but it has many other uses as well. I have never used it with 2D arrays.

http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html

New Answer It seems that the person was asking something more fundamental.

The question was how could YOU implement something that allows a function (such as where) to know what was requested.

First note that calling any of the comparison operators do an interesting thing.

a > 1000
array([False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False, False,
       False, False, False, False, False, False, False, False,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True`,  True,  True,  True,  True,  True,  True,  True,  True,  True], dtype=bool)`

This is done by overloading the "__gt__" method. For instance:

>>> class demo(object):
    def __gt__(self, item):
        print item


>>> a = demo()
>>> a > 4
4

As you can see, "a > 4" was valid code.

You can get a full list and documentation of all overloaded functions here: http://docs.python.org/reference/datamodel.html

Something that is incredible is how simple it is to do this. ALL operations in python are done in such a way. Saying a > b is equivalent to a.gt(b)!

🌐
StrataScratch
stratascratch.com › blog › exploring-numpy-where-in-python
Exploring NumPy where() in Python for Conditional Operations - StrataScratch
September 17, 2025 - The fundamental syntax of NumPy where appears as follows: ... At its core, it checks the condition. If the condition is true, it selects values from x. If it's not, it selects values from y. But you can also use it with just one input to find the indices where the criteria are met.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.where.html
numpy.where — NumPy v2.1 Manual
[xv if c else yv for c, xv, yv in zip(condition, x, y)] Examples · >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> np.where([[True, False], [True, True]], ...
🌐
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.where.html
numpy.where — NumPy v2.4 Manual
Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-numpy-where
How to use Python numpy.where() Method | DigitalOcean
August 3, 2022 - Leverage NumPy’s where() function to efficiently select elements from arrays based on conditions, creating new arrays with tailored values.
🌐
Board Infinity
boardinfinity.com › blog › numpy-where-in-python
numpy.where() in python | Board Infinity
August 30, 2025 - Those functions are numpy array functions. One of the function is numpy.where(). It is used to return the indices of elements in an input array where the given condition is satisfied.
🌐
Real Python
realpython.com › numpy-where-conditional-expressions
How to Use Conditional Expressions With NumPy where() – Real Python
July 12, 2026 - When you write code such as test_array > -2, Python creates a Boolean array behind the scenes. 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.
🌐
OpenSourceOptions
opensourceoptions.com › how-to-use-numpy-where-in-python-with-examples
How to Use numpy.where() in Python with Examples – OpenSourceOptions
The where function from numpy is a powerful way to vectorize if/else statements across entire arrays. There are two primary ways to use numpy.where. First, numpy.where can be used to idenefity array indices where a condition is true (or false).
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.where.html
numpy.where — NumPy v2.0 Manual
Return elements chosen from x or y depending on condition · When only condition is provided, this function is a shorthand for np.asarray(condition).nonzero(). Using nonzero directly should be preferred, as it behaves correctly for subclasses. The rest of this documentation covers only the ...