Iterate NumPy Array Using numpy.nditer()

numpy.nditer() function allow us to iterate a numpy array. In this tutorial, we will introduce how to iterate a numpy array using this function.

Iterate NumPy Array Using numpy.nditer()

1. Import numpy library

import numpy as geek

2. Create 3 * 4 array

a = np.arange(12)
# shape array with 3 rows and 4 columns
 
a = a.reshape(3,4)

a is a numpy array, it is:

[[ 0 1 2 3]

 [ 4 5 6 7]

 [ 8 9 10 11]]

3. Iterate a numpy array using numpy.nditer() with C-style order

for x in geek.nditer(a, order = 'C'): 
 
    print(x)

The output will be:

0 1 2 3 4 5 6 7 8 9 10 11

4. Iterate a numpy array using numpy.nditer() with F-style order

for x in geek.nditer(a, order = 'F'): 
 
    print(x)

The output will be:

0 4 8 1 5 9 2 6 10 3 7 11