## Introduction

Iterating over an array means going through the array element by element. We can easily perform this using the Python for loop. However, for large multidimensional arrays, there are certain tools available in NumPy to speed up the process.

## Iterating Over a 1-D Array

Once again, we begin by first importing the NumPy package and creating a random 1-D array using the numpy.random.choice function and seeding the PRNG so that results are reproducible by the reader.

Example

Creating a 1-D random array.

1import numpy as np
2R = np.random.RandomState(18) #set a random seed
3arr = R.choice(10, size=10, replace=False)
4print(arr)

[7 9 0 4 2 1 6 5 8 3]


We now iterate over the elements of arr using a for loop.

Example

Iterating over a 1-D array.

1for x in arr:
2  print(x)

7
9
0
4
2
1
6
5
8
3


We can also provide an index for each element using the enumerate function.

Example

Iterating over a 1-D array (with an index).

1for x in enumerate(arr):
2    print(x)

(0, 7)
(1, 9)
(2, 0)
(3, 4)
(4, 2)
(5, 1)
(6, 6)
(7, 5)
(8, 8)
(9, 3)


However, for NumPy arrays, there is actually a faster built-in function - numpy.ndenumerate() to return an iterator.

Example

Iterating over a 1-D array using numpy.ndenumerate().

1for (i,x) in np.ndenumerate(arr):
2    print(i, x)

(0,) 7
(1,) 9
(2,) 0
(3,) 4
(4,) 2
(5,) 1
(6,) 6
(7,) 5
(8,) 8
(9,) 3


The advantages of the numpy.ndenumerate() function may not seem obvious for the above 1-D array. However, for multidimensional arrays, it does away with the need for nested loops as the following sections will elaborate.

## Iterating Over a 2-D Array

Iterating over a 2-D array is similar to iterating over a 1-D array, except that 2 for loops are required.

Example

Creating a 2-D random array.

1arr2 = R.choice(12, size=(3,4), replace=False)
2print(arr2)

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


We now iterate over the elements of arr2 using 2 for loops.

Example

Iterating over the elements of a 2-D array.

1for j in arr2: # for each row
2    for i in j:  # for each element of the row
3        print(i)

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


To return the indices and values at the same time, we can invoke the numpy.ndenumerate() function once again.

Example

Iterating over the elements of a 2-D array using numpy.ndenumerate().

1for (i,x) in np.ndenumerate(arr2):
2    print(i, x)

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


## Iterating Over a 3-D Array

Iterating over a 3-D array is similar to iterating over a 2-D array, except that 3 for loops are now required.

Example

Creating a 3-D random array.

1arr3 = R.choice(12, size=(2,3,2), replace=False)
2print(arr3)

[[[11  2]
[ 0  6]
[ 9  5]]

[[ 8  3]
[ 1 10]
[ 4  7]]]


We may iterate over the elements of arr3 using 3 for loops.

Example

Iterating over the elements of a 3-D array.

1for k in arr3: # for each frame
2    for j in k:  # for each row in frame
3        for i in j  # for each element in row
4            print(i)

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


To return the indices and values at the same time, we may once again invoke the numpy.ndenumerate() function.

Example

Iterating over the elements of a 3-D array using numpy.ndenumerate().

1for (i,x) in np.ndenumerate(arr3):
2    print(i, x)

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


In fact, the indices printed above give us an insight into the order in which the elements are iterated over.

We have now witnessed the power of the numpy.ndenumerate() function in iterating over multidimensional arrays. It is our method of choice when it comes to NumPy array iteration, especially large multidimensional arrays.