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 › 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]], ...
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
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
How do I get all even indices in multidimensional numpy array?
Probably not the most efficient, but how about this? arr[np.sum(np.indices(arr.shape), axis=0) % 2 == 0] UPDATE: If your goal is just to access the elements of the array, I don't think you'll find anything faster or more efficient than this. arr.ravel()[::2] More on reddit.com
🌐 r/learnpython
7
10
January 31, 2022
🌐
IONOS
ionos.com › digital guide › websites › web development › python np.where method
What is np.where in Python? - IONOS
January 2, 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 ...
🌐
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])
🌐
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])
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-numpy-where
How to use Python numpy.where() Method | DigitalOcean
Leverage NumPy’s where() function to efficiently select elements from arrays based on conditions, creating new arrays with tailored values.
🌐
Programiz
programiz.com › python-programming › numpy › methods › where
NumPy where()
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.
Find elsewhere
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)!

🌐
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.
🌐
DataCamp
datacamp.com › doc › numpy › where
NumPy where()
import numpy as np x = np.array([1, 2, 3, 4, 5]) y = np.array([10, 20, 30, 40, 50]) result = np.where(x > 3, x, y) print(result) In this example, elements from `x` are selected where `x > 3`, and from `y` otherwise, resulting in `array([10, 20, 30, 4, 5])`.
🌐
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.
🌐
Python Guides
pythonguides.com › python-numpy-where
Np.where In Pandas Python
May 16, 2025 - ... Font ColorwhiteFont Opacity100%Font Size100%Font FamilyArialText ShadownoneBackground ColorblackBackground Opacity50%Window ColorblackWindow Opacity0% ... The np.where() function comes from NumPy but works seamlessly with Pandas. It’s essentially Python’s version of the IF-THEN-ELSE ...
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › where()
Python Numpy where() - Conditional Element Search
December 30, 2024 - The numpy.where() function is a versatile tool in the Python numpy library, primarily used to locate elements or indices in an array that meet certain conditions.
🌐
AskPython
askpython.com › python-modules › numpy › python-numpy-where
np.where in Python: Find Indices and Replace Values with numpy.where() - AskPython
2 weeks ago - 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
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › numpy where() function with examples
NumPy where() Function With Examples - Spark By {Examples}
March 27, 2024 - Python NumPy where() function is used to return the indices of elements in an input array where the given condition is satisfied. Use this function to
🌐
Real Python
realpython.com › numpy-where-conditional-expressions
How to Use Conditional Expressions With NumPy where() – Real Python
July 12, 2026 - If you think this all sounds similar to Python’s ternary operator, you’re correct. The selection logic is the same, though the argument order differs. A conditional expression puts the condition in the middle, whereas np.where() takes the condition first. Note: In this tutorial, you’ll work with two-dimensional arrays. However, the same principles can be applied to arrays of any dimension. Before you start, you should familiarize yourself with NumPy arrays and how to use them.
🌐
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 - Suppose you have two one-dimensional arrays: x and y, as shown in the code snippet. If the condition x > 2 holds true, the new array selects elements from the x array. Otherwise, if the condition is false, the new array selects elements from the y array. import numpy as np x = np.array([1, 2, 3, 4, 5, 6]) y = np.array([10, 20, 30, 40, 50, 60]) result = np.where(x > 2 , x , y) print(result)
🌐
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]], ...
🌐
CodeForGeek
codeforgeek.com › numpy-where-in-python
numpy.where() in Python: Introduction, Syntax & Examples | CodeForGeek
October 26, 2023 - The numpy.where() function in Python's NumPy library is used for element-wise conditional operations on arrays. It allows us to apply conditions to elements
🌐
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 ...