Here's a simple function which returns the coordinates as a tuple (or None if no index is found). Note that this is for 2D matrices, and returns the first instance of the element in the matrix.

(Edit: see hiro protagonist's answer for an alternative Pythonic version)

def find(element, matrix):
    for i in range(len(matrix)):
        for j in range(len(matrix[i])):
            if matrix[i][j] == element:
                return (i, j)

Or, if you want to find all indexes rather than just the first:

def findall(element, matrix):
    result = []
    for i in range(len(matrix)):
        for j in range(len(matrix[i])):
            if matrix[i][j] == element:
                result.append((i, j))
    return result

You can use it like so:

A = [[5, 10],
     [15, 20],
     [25, 5]]


find(25, A) # Will return (2, 0)
find(50, A) # Will return None

findall(5, A) # Will return [(0, 0), (2, 1)]
findall(4, A) # Will return []
Answer from FlipTack on Stack Overflow
Discussions

python - Find indices of a value in 2d matrix - Stack Overflow
if you want to find the index of all the occurrence of a val or character in a 2d list, this code may help you and it's readable. Tnq. for i, e in enumerate(board): for j, ee in enumerate(e): if 'd' in ee: print(i, j) ... Assuming you intended the OP's mymatrix to be the board, this doesn't work because the elements of the matrix ... More on stackoverflow.com
🌐 stackoverflow.com
python - Return index of element in matrix - Stack Overflow
Thanks for feedback about my about ... breaking the community guidelines. 2019-10-30T06:39:01.103Z+00:00 ... how does it happen, that values_sought index isn't out of range in 3rd loop? 2019-10-30T15:52:33.417Z+00:00 ... And why break is in IF statement, if break now breaks loop after function finds first number in matrix... More on stackoverflow.com
🌐 stackoverflow.com
python - How can I get the index of an element from a loop, sourced from a numpy array? - Stack Overflow
I have a matrix (shown below), and I loop over the elements of the matrix with the followng code. for column in m: for elem in column: ... How can I get the index of elem in the matrix?... More on stackoverflow.com
🌐 stackoverflow.com
python - How can I get the index of an element in a matrix from a list that is generated from that matrix? - Stack Overflow
in this code, m is a matrix. x and y are the coordinates of 1 in the matrix, whose POV this code is of. I created a loop that is nested within another loop, that adds each element in the matrix that More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-index-of-element-in-array-in-python
Find index of element in array in python - GeeksforGeeks
July 23, 2025 - We can use an index() method or a simple for loop to accomplish this task. index() method is the simplest way to find the index of an element in an array. It returns the index of the first occurrence of the element we are looking for.
Top answer
1 of 3
2

First, .append() with 2 arguments isn't going to work. You probably meant to append a tuple or list consisting of ri and ei.

Also, please don't use built–in names like list for variables – that's confusing for everyone, including the interpreter.

And as for solving your problem in an efficient manner, it would be best to iterate over list (here values_sought) not in the outer–, but in the innermost loop, so as to avoid pointless checking of the same matrix coordinates multiple times, like this:

values_sought = [1, 2, 3]
matrix = [[0,1,2], [3,2,0], [1,2,3]]
coordinates = []

for row_index, row in enumerate(matrix):
    if not values_sought: break
    for column_index, value_present in enumerate(row):
        if not values_sought: break
        for value_sought_index, value_sought in enumerate(values_sought):
            if value_present == value_sought:
                coordinates.append((row_index, column_index))
                values_sought.pop(value_sought_index)
                break

Values are removed from values_sought after being found, so you may want to have a temporary list for that purpose if you need to still have these values afterwards.

2 of 3
1

There are 2 solutions:

  1. Use two break keywords as in @Vilius Klakauskas response. Here you can read more about using else with for and break statements.

or

  1. Declare a function with return as below:
def find(i): 
    for ri, row in enumerate(matrix): 
        for ei, elem in enumerate(row): 
            if i == matrix[ri][ei]: 
                return (ri, ei) 

and call it in a loop:

coordinates = []
for i in list: 
    coordinates.append(find(i))

coordinates output:

[(0, 1), (0, 2), (1, 0)]

🌐
Stack Overflow
stackoverflow.com › questions › 62649409
python - How can I get the index of an element from a loop, sourced from a numpy array? - Stack Overflow
The documentation is pretty clear and it's easy to see if you print it ... If you enumerate over the rows and columns you will get the indices. If you just want the elements, you already have them in elem.
Find elsewhere
🌐
All About AI-ML
indhumathychelliah.com › 2020 › 10 › 07 › how-to-index-data-in-python-numpy-arrays
How to Index Data in Python Numpy Arrays – All About AI-ML
January 2, 2022 - Indexing and slicing Photo by Magda Ehlers from Pexels Basic Indexing Basic Indexing can be done to access a particular element from the NumPy arrays. 1. One-dimensional array Creating a one-dimensional array import numpy as np one_d=np.array([10,20,30,40]) print (one_d) #Output:array([10, 20, 30, 40]) Image by Author Accessing elements from the array by positive indexing In Python, all…
🌐
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 ...
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-find-indices-of-a-given-value-in-a-numpy-array-or-matrix-in-python-
How to find indices of a given value in a numpy array (or matrix) in python ?
April 13, 2021 - Let's create a 1d matrix with numpy (for that we generate random integers between [0,10[ for a matrix of dimensions (8,)) import numpy as np a = np.random.randint(10, size=(8,)) print(a) ... Here 3 is at the indices 1 and 7.
🌐
Stack Overflow
stackoverflow.com › questions › 60087846 › get-matrix-index-from-array-index
python - Get matrix index from array index - Stack Overflow
February 6, 2020 - [Tryouts]$ python TestMat.py Row in matrix: 2 Column in matrix: 1 · I have assumed matrix with zero based index. If your index starts with 1 then simply add 1 to row and column index. ... Find the answer to your question by asking.
🌐
W3Schools
w3schools.com › python › NumPy › numpy_array_indexing.asp
NumPy Array Indexing
You can access an array element by referring to its index number.
🌐
NumPy
numpy.org › devdocs › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.6.dev0 Manual
The simplest case of indexing with N integers returns an array scalar representing the corresponding item. As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array.
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-index-of-element-in-array-python
Find Index of Element in Array - Python - GeeksforGeeks
July 23, 2025 - It is simple to use and works well when we know the element exists in the array. It's efficient for arrays that do not have duplicate elements since it returns the first occurrence of the element. ... import array arr = array.array('i', [10, 20, 30, 40, 50]) # Find the index of the element 30 idx = arr.index(30) print(idx)
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-search-elements-in-a-matrix
Search Elements in a Matrix - Python - GeeksforGeeks
July 11, 2025 - Explanation: 19 in itertools.chain(*a) flattens the 2D list a and checks if 19 is present in the flattened sequence, offering a efficient way to search for the element. This is the most explicit method, where we manually iterate through the matrix using a nested for loop.
🌐
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.
🌐
Python Guides
pythonguides.com › find-the-index-of-an-element-in-an-array-in-python
How To Find The Index Of An Element In An Array In Python?
July 22, 2026 - The simplest approach uses the list index() method. A method is a function attached to an object, such as a Python list. The index() method searches from left to right. It returns the zero-based index of the first matching element.
🌐
Medium
medium.com › @whyamit404 › basics-of-numpy-array-indexing-9052e6d6b5cf
Basics of NumPy Array Indexing. If you think you need to spend $2,000… | by whyamit404 | Medium
February 9, 2025 - It fetches the element at row 0 and column 1. matrix[2, 0]: The third row (2) and first column (0) give us the number 7. Pro tip: If you’ve worked with Excel, think of rows and columns like cell coordinates. Here’s a trick you’ll love — negative indexing.