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
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.where.html
numpy.where — NumPy v2.5.dev0 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-where-in-python
numpy.where() in Python - GeeksforGeeks
September 30, 2025 - With only condition: returns a tuple of index arrays (one per dimension). With x and y: returns a new array choosing from x where True, y where False (supports broadcasting and dtype rules).
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
`where` clauses in List comprehension - Ideas - Discussions on Python.org
Has the where clause, as used in Haskell, ever been explored for Python List comprehension? For example, text_numbers = [ "one", "two", "three", .... ] # Print pair numbers print( [ x for x in text_numbers … More on discuss.python.org
🌐 discuss.python.org
0
November 9, 2022
How do I select certain values from a table for my code in Python?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
7
3
April 9, 2024
Adding the method find() to list - Ideas - Discussions on Python.org
Before I talk about the real topic, I have to ask if the discussion of enhancements should start here, because I already created a issue in the issue tracker. It is Ok to just create it, or the discussion should start here? Now the real topic: . . . “”" PROBLEM: When trying to search the ... More on discuss.python.org
🌐 discuss.python.org
0
May 6, 2020
🌐
W3Schools
w3schools.com › python › pandas › ref_df_where.asp
Pandas DataFrame where() Method
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
StrataScratch
stratascratch.com › blog › exploring-numpy-where-in-python
Exploring NumPy where() in Python for Conditional Operations - StrataScratch
September 17, 2025 - With three arguments: when you ... large arrays · Let's start with something simple. When you offer np.where() a condition, it gives you the indices where that condition is true....
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.4 documentation
February 27, 2026 - If the named attribute does not ... not be a Python identifier (see setattr()). ... Since private name mangling happens at compilation time, one must manually mangle a private attribute’s (attributes with two leading underscores) name in order to retrieve it with getattr(). ... Return the dictionary implementing the current module namespace. For code within functions, this is set when the function is defined and remains the same regardless of where the function ...
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)!

Find elsewhere
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.4 documentation
[1] This is a design principle ... in Python. Another thing you might notice is that not all data can be sorted or compared. For instance, [None, 'hello', 10] doesn’t sort because integers can’t be compared to strings and None can’t be compared to other types. Also, there are some types that don’t have a defined ordering relation. For example, 3+4j < 5+7j isn’t a valid comparison. The list methods make it very easy to use a list as a stack, where the last element ...
🌐
IONOS UK
ionos.co.uk › digital guide › websites › web development › python np.where method
What is np.where in Python? - IONOS UK
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­res­pond ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-numpy-where
How to use Python numpy.where() Method | DigitalOcean
August 3, 2022 - In Python, we can use the numpy.where() function to select elements from a numpy array, based on a condition.
🌐
Programiz
programiz.com › python-programming › numpy › methods › where
NumPy where()
If we pass a single argument (test condition) to numpy.where(), it tells us where in a given array the given condition is met by returning the indices.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.where.html
numpy.where — NumPy v2.2 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 ...
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › where
Python Numpy where() - Conditional Element Search | Vultr Docs
December 30, 2024 - The numpy.where() function in Python offers a robust way to perform conditional element searches within arrays, enabling efficient and selective data handling. From simple condition checks to complex analyses involving multiple conditions and ...
🌐
DataCamp
datacamp.com › doc › numpy › where
NumPy where()
In this syntax, `condition` is a boolean array, and `x` and `y` are optional arrays or scalars from which elements are selected based on `condition`. When only `condition` is provided, `where()` returns the indices of elements where `condition` is `True`.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 3.0.2 documentation
Where cond is True, keep the original value. Where False, replace with corresponding value from other. If cond is callable, it is computed on the Series/DataFrame and should return boolean Series/DataFrame or array. The callable must not change input Series/DataFrame (though pandas doesn’t check it).
🌐
Python.org
discuss.python.org › ideas
`where` clauses in List comprehension - Ideas - Discussions on Python.org
November 9, 2022 - Has the where clause, as used in Haskell, ever been explored for Python List comprehension? For example, text_numbers = [ "one", "two", "three", .... ] # Print pair numbers print( [ x for x in text_numbers if num % 2 == 0 where num = get_number_from_text(x) ] )
🌐
Reddit
reddit.com › r/learnprogramming › how do i select certain values from a table for my code in python?
r/learnprogramming on Reddit: How do I select certain values from a table for my code in Python?
April 9, 2024 -

I know this seems like a strange question but I'm new to coding. Wondering if there is a simple way to address individual values in the table. I was able to web scrape it but just trying to figure out how to select certain values. I scraped a table to show weaknesses and strengths for pokemon types and want to make a for loop with an input that spits back the pokemon typing's characteristics. I understand I could do this all manually without a table but I wanted to try something new to understand how it works. Any help is nice, Thanks.

import pandas as pd

tables = pd.read_html('https://www.eurogamer.net/pokemon-go-type-chart-effectiveness-weaknesses')

print(tables[0])

this code will set up a table from the website. Thanks!

🌐
Python.org
discuss.python.org › ideas
Adding the method find() to list - Ideas - Discussions on Python.org
May 6, 2020 - Now the real topic: . . . “”" PROBLEM: When trying to search the position of an element inside a list, we should use the in operator to first check if the element exists, and then use the index method to obtain the index. in (__contains__) ...
🌐
DataCamp
datacamp.com › tutorial › python-list-index
Python List index() Method Explained with Examples | DataCamp
March 28, 2025 - Learn how to use Python's index() function to find the position of elements in lists. Includes examples, error handling, and tips for beginners.