You can use np.where to return a tuple of arrays of x and y indices where a given condition holds in an array.

If a is the name of your array:

>>> np.where(a == 1)
(array([0, 0, 1, 1]), array([0, 1, 2, 3]))

If you want a list of (x, y) pairs, you could zip the two arrays:

>>> list(zip(*np.where(a == 1)))
[(0, 0), (0, 1), (1, 2), (1, 3)]

Or, even better, @jme points out that np.asarray(x).T can be a more efficient way to generate the pairs.

Answer from Alex Riley on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › help finding the index of a given value within a 2d array
r/learnpython on Reddit: Help finding the index of a given value within a 2D array
November 13, 2022 -

Been messing around with numpy, trying to familiarize myself with it and seeing how I'd be able to utilize its arrays to store (very simple) map data for this text adventure game I've been working on. So far it seems like it'd be pretty darn useful, but I seem to have run into something of an issue.

My code is as follows (note: this is purposefully made with out a main class, because I am just trying to get the basic functionality down in my head before I incorporate it into my main program, and this is just easier for me):

# import numpy module as np
import numpy as np

# Establish the game map, a 3x3 grid of 0's
mainarray = np.array([[0, 0, 0],
                      [0, 0, 0],
                      [0, 0, 0]])

# Give feedback to make console easier to read
print(mainarray)
print("")
print("Placing player in center...")

# Place player on map (represented by a value of 1)
mainarray[1, 1] = 1

print(mainarray)

print("")
print("Locating player...")

# Attempt to find what the current index is of the value 1
print("")
print("Player is at index: ", np.where(mainarray == 1)[0][0])

In my head, I would like to eventually use this np.where() function (if I can) in one of the functions that moves my character. What I want to do is grab the current index of the "player" (represented by the number 1) and attempt to change that value to a 0, and change the value at an adjacent index to 1 (the tile that the player is moving to). Basically, I would like to use each index of the array as a sort of coordinate that I can take and use to move this "1" around the array, setting each index back to "0" after moving the "1", all based off user input.

Anyways, I am not getting any errors, however, the result that gets printed to the console is:

Player is at index:  1

Why is it just one number? It remains the same, even when I remove that second [0] in the last line. I don't fully understand how this function works, as this is the first time I've tried to use it, but since there are no errors, I have no clue what is going on.

Shouldn't I be receiving an index with two values, one for the column and one for the row? If not, how can I grab that as a result and use it in the way I described above? Is it even possible, or am I barking up the wrong tree with this function?

Discussions

python - Finding indices of values in 2D numpy array - Stack Overflow
I'm trying to get the index values out of a numpy array, I've tried using intersects instead to no avail. I'm simply trying to find like values in 2 arrays. One is 2D and I'm selecting a column, an... More on stackoverflow.com
🌐 stackoverflow.com
June 26, 2018
using np.where on a 2D array
Do an all-reduction after comparing the two arrays: index_that_matches = np.where((reduced == vector).all(1))[0][0] More on reddit.com
🌐 r/learnpython
3
2
September 5, 2022
How to find index of value in NumPy array? - Data Exploration & Visualization - Data Science Dojo Discussions
Suppose I have an array that contains zero and non-zero values. Now I want to find the index of non-zero values of my array. I tried many methods previously, but they are not giving me the desired result. Can someone help me with this? More on discuss.datasciencedojo.com
🌐 discuss.datasciencedojo.com
3
0
February 17, 2023
python - Find the index of a value in a 2D array - Stack Overflow
As a result I get “ValueError: ‘Tragedy’ is not in list · I’d come to the conclusion that .index only works for 1D arrays? But then how do I do it die 2D arrays? This code works fine if I make the array 1D. More on stackoverflow.com
🌐 stackoverflow.com
🌐
YouTube
youtube.com › codesolve
numpy find index of value in 2d array - YouTube
Download 1M+ code from https://codegive.com finding the index of a value in a 2d array using numpy is a common task in data analysis and scientific computin...
Published: November 16, 2024
Views: 9
🌐
CodeSpeedy
codespeedy.com › home › find the index of value in numpy array
Find the index of value in Numpy Array - CodeSpeedy
October 4, 2022 - Learn how to find the index of value in Numpy array using the numpy.where() and argsort+searchsorted() function on 1 and 2 dimensional array.
🌐
NumPy
numpy.org › devdocs › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.6.dev0 Manual
The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (\(k\neq0\)). This selects the m elements (in the corresponding dimension) with index values i, i + k, …, i + (m - 1) k where \(m = q + (r\neq0)\) and q and r are the quotient and remainder ...
Top answer
1 of 2
11

np.where with a single argument is equivalent to np.nonzero. It gives you the indices where a condition, the input array, is True.

In your example you are checking for element-wise equality between a[:,1] and values

a[:, 1] == values
False

So it's giving you the correct result: no index in the input is True.

You should use np.isin instead

np.isin(a[:,1], values)
array([False, False,  True,  True, False, False, False, False,  True, False], dtype=bool)

Now you can use np.where to get the indices

np.where(np.isin(a[:,1], values))
(array([2, 3, 8]),)

and use those to address the original array

a[np.where(np.isin(a[:,1], values))]    
array([[    1, 97612,     1],
       [    1, 97697,     1],
       [    1, 97944,     1]])

Your initial solution with a simple equality check could indeed have worked with proper broadcasting:

np.where(a[:,1] == values[..., np.newaxis])[1]
array([2, 3, 8])

EDIT: given you seem to have issues with using the above results to index and manipulate your array here's a couple of simple examples

Now you should have two ways of accessing your matching elements in the original array, either the binary mask or the indices from np.where.

mask = np.isin(a[:,1], values)  # np.in1d if np.isin is not available
idx = np.where(mask)

Let's say you want to set all matching rows to zero

a[mask] = 0   # or a[idx] = 0
array([[    1, 97553,     1],
       [    1, 97587,     1],
       [    0,     0,     0],
       [    0,     0,     0],
       [    1, 97826,     3],
       [    1, 97832,     1],
       [    1, 97839,     1],
       [    1, 97887,     1],
       [    0,     0,     0],
       [    1, 97955,     2]])

Or you want to multiply the third column of matching rows by 100

a[mask, 2] *= 100
array([[    1, 97553,     1],
       [    1, 97587,     1],
       [    1, 97612,   100],
       [    1, 97697,   100],
       [    1, 97826,     3],
       [    1, 97832,     1],
       [    1, 97839,     1],
       [    1, 97887,     1],
       [    1, 97944,   100],
       [    1, 97955,     2]])

Or you want to delete matching rows (here using indices is more convenient than masks)

np.delete(a, idx, axis=0)
array([[    1, 97553,     1],
       [    1, 97587,     1],
       [    1, 97826,     3],
       [    1, 97832,     1],
       [    1, 97839,     1],
       [    1, 97887,     1],
       [    1, 97955,     2]])
2 of 2
1

Just a thought:

Try to flatten the 2D array and compare using numpy.intersect1d.

https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.ndarray.flatten.html

https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.intersect1d.html

🌐
W3Schools
w3schools.com › python › numpy › numpy_array_indexing.asp
NumPy Array Indexing
The third number represents the third dimension, which contains three values: 4 5 6 Since we selected 2, we end up with the third value: 6 · Use negative indexing to access an array from the end. ... import numpy as np arr = np.array([[1,2,3,4,5], [6,7,8,9,10]]) print('Last element from 2nd dim: ', arr[1, -1]) Try it Yourself »
🌐
thisPointer
thispointer.com › home › python › find the index of value in numpy array using numpy.where()
Find the index of value in Numpy Array using numpy.where() - thisPointer
April 1, 2023 - Let’s find the indices of element with value 15 in this 2D numpy array i.e. # Get the index of elements with value 15 result = np.where(arr == 15) print('Tuple of arrays returned : ', result)
Find elsewhere
🌐
Quora
quora.com › How-do-I-return-the-index-of-an-element-in-a-2D-array-in-Python
How to return the index of an element in a 2D array in Python - Quora
Answer (1 of 2): You don’t return the index - your code needs to keep track of where the elements are. If you are asking how do you search a 2D array - the only way is something like this: [code]def find_all(matrix, element): """Iterate through all row, column indexes in a 2D Matrix where ...
🌐
Data Science Parichay
datascienceparichay.com › article › find-index-of-element-in-numpy-array
Find Index of Element in Numpy Array
Two thumbs up - I recently switched to WPX Hosting and recommend their speed, service and security - they do know what they are talking about when it comes to WordPress hosting.
🌐
w3resource
w3resource.com › python-exercises › numpy › find-and-Index-elements-in-2d-numpy-array-using-np-dot-nonzero.php
Find and Index elements in 2D NumPy array using np.nonzero
April 29, 2025 - Learn how to create a 2D NumPy array and use np.nonzero to find indices of elements that satisfy a condition, then use these indices for advanced indexing. Follow our step-by-step guide.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-find-the-index-of-value-in-numpy-array
How to find the Index of value in Numpy Array ? - GeeksforGeeks
July 23, 2025 - Here, we find all the indexes of 3 and the index of the first occurrence of 3, we get an array as output and it shows all the indexes where 3 is present. ... # import numpy package import numpy as np # create an numpy array a = np.array([1, 2, 3, 4, 8, 6, 7, 3, 9, 10]) # display index value of 3 print("All index value of 3 is: ", np.where(a == 3)[0]) print("First index value of 3 is: ",np.where(a==3)[0][0])
🌐
Reddit
reddit.com › r/learnpython › using np.where on a 2d array
r/learnpython on Reddit: using np.where on a 2D array
September 5, 2022 -

I have a 2D array that is shaped like (10000,2). The rows in this array repeat so I call np.unique with axis=0, to get the 2D array of all the unique rows. This reduces the size down to (24,2) or something like that. Now, I have a vector and I want to find out which index in the reduced array it is equal to. So I call np.where(reduced == vector). This does not give me the row that it is equal. Rather it takes the input vector 1 number at a time and finds where in the reduced array the numbers match. This is OK since when there are two numbers that are the same consecutively, that equates to the index that I want. So I just call np.ediff1d and find where that equals 0. I was wondering if there is an easier way to do this? Thanks. Example code is given.

import numpy as np

reduced = [[10,2],[15,3],[10,1],[16,5],[16,3]]
vector = [15,3]

results = np.where(reduced == vector)

# I want the output to be 1 but I get 
# results[0] = [1,1,4]
diffs = np.ediff1d(results[0]) 

# this results in [0,3]
index_that_matchs = np.where(diffs == 0)[0][0]
index = results[0][index_that_matchs]

# this results in 1
🌐
Data Science Dojo
discuss.datasciencedojo.com › data exploration & visualization
How to find index of value in NumPy array? - Data Exploration & Visualization - Data Science Dojo Discussions
February 17, 2023 - Suppose I have an array that contains zero and non-zero values. Now I want to find the index of non-zero values of my array. I tried many methods previously, but they are not giving me the desired result. Can someone hel…
🌐
Pluralsight
pluralsight.com › blog › tech guides & tutorials
Working with Numpy Arrays: Indexing & Slicing | Pluralsight
Similar to programming languages like Java and C#, the index starts with zero. So to access the third element in the array, use the index 2. ... To access elements in this array, use two indices. One for the row and the other for the column.
🌐
Iditect
iditect.com › faq › python › how-to-find-the-index-of-a-value-in-2d-array-in-python.html
How to find the index of a value in 2d array in Python?
If you need to perform frequent searches in large arrays, you might consider more advanced data structures like dictionaries or using libraries such as NumPy, which provide more efficient ways to handle 2D array operations. How to find the index of a specific value in a 2D array in Python using ...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.indices.html
numpy.indices — NumPy v2.5 Manual
>>> i, j = np.indices((2, 3), sparse=True) >>> i.shape (2, 1) >>> j.shape (1, 3) >>> i # row indices array([[0], [1]]) >>> j # column indices array([[0, 1, 2]])
🌐
Statology
statology.org › home › how to find index of value in numpy array (with examples)
How to Find Index of Value in NumPy Array (With Examples)
September 17, 2021 - This tutorial explains how to find the index location of specific values in a NumPy array, including examples.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_search.asp
NumPy Searching Arrays
You can search an array for a certain value, and return the indexes that get a match. To search an array, use the where() method. ... import numpy as np arr = np.array([1, 2, 3, 4, 5, 4, 4]) x = np.where(arr == 4) print(x) Try it Yourself »