Python NumPy Array – Iterate over all elements

Iterate over elements of NumPy Array

To iterate over a NumPy Array, you can use numpy.nditer iterator object. numpy.nditer provides Python’s standard Iterator interface to visit each of the element in the numpy array. Any dimensional array can be iterated.


In the following example, we have a 2D array, and we use numpy.nditer to print all the elements of the array.

import numpy as np

#2D array
a = (np.arange(8)*2).reshape(2,4)

#print array
print("The array\n",a)

print("\nIterating over all the elemnets of array")
#iterate over elements of the array
for x in np.nditer(a):
	print(x, end=' ')