You get prettier code with:

for iy, ix in np.ndindex(a.shape):
    print(a[iy, ix])

resulting in:

1
2
3
4
5
6
7
8
9
10
11
12
Answer from Markus Dutschke on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_iterating.asp
NumPy Array Iterating
Iterate through every scalar element ... one. Sometimes we require corresponding index of the element while iterating, the ndenumerate() method can be used for those usecases....
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.5 Manual
During iteration, you may want to use the index of the current element in a computation. For example, you may want to visit the elements of an array in memory order, but use a C-order, Fortran-order, or multidimensional index to look up values in a different array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-iterating-over-array
Numpy - Iterating Over Arrays - GeeksforGeeks
July 23, 2025 - For multidimensional arrays, we can use enumerate to access both the index and value during iteration. Python ยท import numpy as np # Create a 2D array arr = np.array([[10, 20], [30, 40]]) # Enumerate through rows for idx, row in enumerate(arr): ...
๐ŸŒ
Kaggle
kaggle.com โ€บ general โ€บ 521858
Numpy Arrays Iterating | Kaggle
# Creating 2D array arr2d = ... ::2]): print(x) content_copy ยท The ndenumerate() function allows us to iterate through a NumPy array while also keeping track of the index of each element....
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_iterating_over_array.htm
NumPy - Iterating Over Array
In NumPy, arrays are indexed starting from 0 for each dimension. Iterating with indices involves using nested loops to traverse through each dimension of the array and access elements using their specific indices.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.1 Manual
During iteration, you may want to use the index of the current element in a computation. For example, you may want to visit the elements of an array in memory order, but use a C-order, Fortran-order, or multidimensional index to look up values in a different array.
Find elsewhere
๐ŸŒ
MindStick
mindstick.com โ€บ interview โ€บ 34411 โ€บ how-to-iterating-numpy-array
How to Iterating NumPy Array? โ€“ MindStick
November 12, 2025 - import numpy as np arr = np.array([1, 2, 3, 4, 5]) for x in arr: print(x) ... You can convert elements while iterating, e.g. from int to float: for x in np.nditer(arr, flags=['buffered'], op_dtypes=['float']): print(x) ... arr = np.array([[1, ...
๐ŸŒ
Python Like You Mean It
pythonlikeyoumeanit.com โ€บ Module3_IntroducingNumpy โ€บ ArrayTraversal.html
Iterating Over Arrays & Array-Traversal Order โ€” Python Like You Mean It
These two orderings are simple ... and so on? The generic rules are as follows: Row-major ordering (C ordering) {NumPyโ€™s default}: traverse an array by advancing the index ......
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.2 Manual
During iteration, you may want to use the index of the current element in a computation. For example, you may want to visit the elements of an array in memory order, but use a C-order, Fortran-order, or multidimensional index to look up values in a different array.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to iterate over a numpy array
How to Iterate over a NumPy Array - Be on the Right Side of Change
August 16, 2022 - The output is sent to the terminal. Then idx displays a Tuple containing the index value of the array and then the value of x for each iteration. This method uses a for loop and range()) to iterate through a 3D NumPy array.
Top answer
1 of 3
19

As I've become more familiar with the NumPy and general scientific array processing ecosystem, it's become clearer to me that iteration is usually outright wrong due to how slow it is in comparison, and writing to use a vectorized operation is best whenever possible. Though the style is not as obvious/Pythonic at first, I've (anecdotally) gained ridiculous speedups with vectorized operations; more than 1000x in a case of swapping out a form like some row iteration .apply(lambda)

@MSeifert's answer much better provides this and will be significantly more performant on a dataset of any real size

More general Answer by @cs95 covering and comparing alternatives to iteration in Pandas


Original Answer

You can iterate through the values in your array with numpy.ndenumerate to get the indices of the values in your array.

Using the documentation above:

A = np.array([[1,2,3],[4,5,6],[7,8,9]])
for index, values in np.ndenumerate(A):
    print(index, values)  # operate here
2 of 3
8

You can do it using np.ndenumerate but generally you don't need to iterate over an array.

You can simply create a meshgrid (or open grid) to get all indices at once and you can then process them (vectorized) much faster.

For example

>>> x, y = np.mgrid[slice(A.shape[0]), slice(A.shape[1])]
>>> x
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2]])
>>> y
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])

and these can be processed like any other array. So if your function that needs the indices can be vectorized you shouldn't do the manual loop!

For example to calculate the lattice distance for each point to a point say (2, 3):

>>> abs(x - 2) + abs(y - 3)
array([[5, 4, 3],
       [4, 3, 2],
       [3, 2, 1]])

For distances an ogrid would be faster. Just replace np.mgrid with np.ogrid:

>>> x, y = np.ogrid[slice(A.shape[0]), slice(A.shape[1])]
>>> np.hypot(x - 2, y - 3)  # cartesian distance this time! :-)
array([[ 3.60555128,  2.82842712,  2.23606798],
       [ 3.16227766,  2.23606798,  1.41421356],
       [ 3.        ,  2.        ,  1.        ]])
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.4 โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.4 Manual
During iteration, you may want to use the index of the current element in a computation. For example, you may want to visit the elements of an array in memory order, but use a C-order, Fortran-order, or multidimensional index to look up values in a different array.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.6.dev0 Manual
During iteration, you may want to use the index of the current element in a computation. For example, you may want to visit the elements of an array in memory order, but use a C-order, Fortran-order, or multidimensional index to look up values in a different array.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.0 โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.0 Manual
During iteration, you may want to use the index of the current element in a computation. For example, you may want to visit the elements of an array in memory order, but use a C-order, Fortran-order, or multidimensional index to look up values in a different array.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.ndenumerate.html
numpy.ndenumerate โ€” NumPy v2.5 Manual
January 31, 2021 - Multidimensional index iterator. Return an iterator yielding pairs of array coordinates and values.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.ndindex.html
numpy.ndindex โ€” NumPy v2.5 Manual
At each iteration a tuple of indices is returned, the last dimension is iterated over first. ... The size of each dimension of the array can be passed as individual parameters or as the elements of a tuple. ... Try it in your browser! ... >>> for index in np.ndindex(3, 2, 1): ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ reference โ€บ arrays.nditer.html
Iterating over arrays โ€” NumPy v2.3 Manual
During iteration, you may want to use the index of the current element in a computation. For example, you may want to visit the elements of an array in memory order, but use a C-order, Fortran-order, or multidimensional index to look up values in a different array.