NumPy Array Itereren

Array iteration

Iteration means to iterate over elements sequentially.

When we are dealing with multi-dimensional arrays in numpy, we can use Python's basic for loops to accomplish this operation.

If we iterate over a 1-D array, it will iterate over each element sequentially.

Example

Iterate over the elements of the following 1-D array:

import numpy as np
arr = np.array([1, 2, 3])
for x in arr:
  print(x)

Run Example

Iterate over the 2-D array

In the 2-D array, it will iterate over all rows.

Example

Iterate over the elements of the following 2-D array:

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
  print(x)

Run Example

If we iterate over an n-D array, it will iterate over the (n-1)th dimension sequentially.

To return actual values, scalars, we must iterate over the array in each dimension.

Example

Iterate over each scalar element of the 2-D array:

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
  for y in x:
    print(y)

Run Example

Iterate over the 3-D array

In the 3-D array, it will iterate over all 2-D arrays.

Example

Iterate over the elements of the following 3-D array:

import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in arr:
  print(x)

Run Example

To return actual values, scalars, we must iterate over the array in each dimension.

Example

Iterate to scalar:

import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
for x in arr:
  for y in x:
    for z in y:
      print(z)

Run Example

use nditer() to iterate over the array

function nditer() is a helper function that can be used from very basic iteration to very advanced iteration. It solves some of the basic problems we face in iteration, and let's introduce it through examples.

iterate over each scalar element

In the basic for In the loop, to iterate over each scalar in the array, we need to use n for Loops can be difficult to write for arrays with high dimensions.

Example

Iterate over the following 3-D array:

import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
for x in np.nditer(arr):
  print(x)

Run Example

to iterate over arrays of different data types

We can use op_dtypes Parameters, and pass the expected data type, to change the data type of elements during iteration.

NumPy does not change the data type of elements in place (elements are located in the array), so it needs some other space to perform this operation, this additional space is called buffer, to nditer() Enable it in the middle, we pass parameters flags=['buffered'].

Example

Traverse the array in string form:

import numpy as np
arr = np.array([1, 2, 3])
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S'])
  print(x)

Run Example

Iterate with different step lengths

We can use filtering and then iterate.

Example

Skip 1 element every time a scalar element of the 2D array is traversed:

import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
for x in np.nditer(arr[:, ::2]):
  print(x)

Run Example

Use ndenumerate() for enumeration iteration

Enumeration refers to the serial mention of things.

Sometimes, when iterating, we need the corresponding index of the element, for these cases, we can use ndenumerate() Method.

Example

Enumerate the elements of the following 1D array:

import numpy as np
arr = np.array([1, 2, 3])
for idx, x in np.ndenumerate(arr):
  print(idx, x)

Run Example

Example

Enumerate the elements of the following 2D array:

import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
for idx, x in np.ndenumerate(arr):
  print(idx, x)

Run Example