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
Discussions

Find Element In Two-Dimensional Python Array - Stack Overflow
What I am trying to do is take a user input for as many rows as they wish, and make it into an array. After that i want to find where the highest number is in the array, an (x,y) coordinate of the ... More on stackoverflow.com
🌐 stackoverflow.com
How to find a value in a 2D array in python? - Stack Overflow
I'm making a board game for school and I would like to be able to find the index of the place number they have and replace the number on the board with their counter ("x" or "y"). board = [ ["... More on stackoverflow.com
🌐 stackoverflow.com
July 18, 2017
How to find the row and column of element in 2d array in python? - Stack Overflow
When you find the matching element, return the current indexes. ... I understand, and thankyou for that @Barmar, i will update the question and next time wont be again like this. appologies im new here for asking ... and also, this is my very first time trying to learn python. More on stackoverflow.com
🌐 stackoverflow.com
searching a 2d array in python - best method + indentation error - Stack Overflow
I have created the following 2d array (list of lists) in Python: #creating a 2d array (3 rows by 7 columns) and populating it with numbers matrix=[1,2,3,4,5,6,7],[8,9,10,11,12,13,14],[15,16,17,18,... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 ...
Find elsewhere
Top answer
1 of 8
4

The main problem here is that break only exits the innermost loop. So, if an element is found, break will skip checking other elements in the same column, but still the outer loop will advance to next row. What you really want is either this:

found = False
for row in matrix:
    for element in row:
        if element == number:
            found = True
            break
    if found:
        break
if found:
    print("Found")
else:
    print("Not found")

(notice the other break) or, possibly, a more readable solution using a function:

def searchfor(matrix, number):
    for row in matrix:
        for element in row:
            if element == number:
                return True
    return False

if searchfor(matrix, number):
    print("Found")
else:
    print("Not found")

Edit: It just occured to me that it is possible to write it without either a flag variable or a function, but it is not a particularly elegant way. Still, for completeness, here you are:

for row in matrix:
    for element in row:
        if element == number:
            break
    else:
        continue
    break

if element == number:
    print("Found")
else:
    print("Not found")

The continue statement will execute only if the inner loop was not exited by break, and it will advance the outer loop to the next row; otherwise the second break will end the outer loop.

2 of 8
1

You seem to be new to Python. In this language, code blocks are identified by the number of indents you have before an instruction. In your case, you have an if statement, but your else is not matching the indentation of that if statement.
You'd want your code to be something like this -

number=int(input("What number are you looking for?"))
flag = False
for i in range(rows):
      for j in range(columns):
        if matrix[i][j]==number:
          print("Found it!")
          flag = True
          break
if flag == False:
  print ("Not found!")
🌐
Scaler
scaler.com › home › topics › 2d array in python
2D Array in Python | Python Two-Dimensional Array - Scaler Topics
May 25, 2026 - Unlike a one dimensional array, which uses a single index for individual elements, a 2D array uses two indices to locate a value. 2D arrays in python are zero-indexed, which means counting indices start from zero rather than one; thus, zero is the first index in an array in python.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AccessingDataAlongMultipleDimensions.html
Accessing Data Along Multiple Dimensions in an Array — Python Like You Mean It
NumPy specifies the row-axis (students) of a 2D array as “axis-0” and the column-axis (exams) as axis-1. You must now provide two indices, one for each axis (dimension), to uniquely specify an element in this 2D array; the first number specifies an index along axis-0, the second specifies an index along axis-1.
🌐
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?

🌐
Javatpoint
javatpoint.com › python-2d-array
Python 2D array - Javatpoint
January 10, 2021 - Python 2D array with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
🌐
Python
mail.python.org › pipermail › tutor › 2005-June › 039149.html
[Tutor] Checking if value exist in a '2D array'
July 17, 2005 - > > if list[[x][0]] == 'value': # where x can be anything > 0 > print 'found' > > So, is there a similar method like list.count('value') that I use for 1D lists? I would use 'value' in list rather than list.count('value') because it will stop when it finds 'value'; list.count() will always inspect the whole list. Kent · Previous message: [Tutor] Checking if value exist in a '2D array'
🌐
Stack Overflow
stackoverflow.com › questions › 68355404 › how-to-find-specific-elements-in-2d-array
python - How to find specific elements in 2d array? - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. Copya2D = np.arraya2D = np.array([[1, 2, 3, 2, 1], [1, 4, 5, 3, 2]])