Skip to main content

Introduction to Python NumPy Array Iterating

Python NumPy Array Iterating

There are several ways to iterate over arrays. Let's explore some common methods for iterating through NumPy arrays:

Using nditer()

The nditer() function allows you to iterate over each element of a NumPy array.

It provides flexible options for controlling the iteration behavior, such as specifying the order, data type, and more.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

# Iterate over each element using nditer()
for element in np.nditer(arr):
print(element)

Output:

1
2
3
4
5
6

Using ndenumerate()

The ndenumerate() function combines iterating over the elements of an array with their corresponding indices.

It returns an iterator yielding both the index and value of each element.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

# Iterate over each element with its index using ndenumerate()
for index, value in np.ndenumerate(arr):
print(index, value)

Output:

(0, 0) 1
(0, 1) 2
(0, 2) 3
(1, 0) 4
(1, 1) 5
(1, 2) 6

Using nditer() with different iteration orders

You can control the iteration order using the order parameter of nditer().

For example, you can iterate over the elements row by row or column by column.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

# Iterate row-wise using nditer()
for element in np.nditer(arr, order='C'): # 'C' for row-wise, 'F' for column-wise
print(element)

Output:

1
2
3
4
5
6

Iterating 2-D Arrays

When iterating over a 2-dimensional NumPy array, you can use nested loops or the nditer() function with appropriate parameters.

Using nested loops

You can use nested loops to iterate over each element in a 2D array.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

# Iterate over each element using nested loops
for row in arr:
for element in row:
print(element)

Output:

1
2
3
4
5
6

Using nditer() with multi_index option

The nditer() function can be used with the multi_index option to access both the element and its indices during iteration.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

# Iterate over each element with indices using nditer() and multi_index
with np.nditer(arr, flags=['multi_index']) as it:
while not it.finished:
index = it.multi_index
element = arr[index]
print(index, element)
it.iternext()

Output:

(0, 0) 1
(0, 1) 2
(0, 2) 3
(1, 0) 4
(1, 1) 5
(1, 2) 6

Using nditer() with ndenumerate()

You can combine the nditer() function with ndenumerate() to iterate over each element with its corresponding indices.

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

# Iterate over each element with indices using nditer() and ndenumerate
for index, element in np.ndenumerate(arr):
print(index, element)

Output:

(0, 0) 1
(0, 1) 2
(0, 2) 3
(1, 0) 4
(1, 1) 5
(1, 2) 6

Iterating 3-D Arrays

When iterating over a 3-dimensional NumPy array, you can use nested loops or the nditer() function with appropriate parameters.

Using nested loops

You can use nested loops to iterate over each element in a 3D array.

import numpy as np

arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Iterate over each element using nested loops
for plane in arr:
for row in plane:
for element in row:
print(element)

Output:

1
2
3
4
5
6
7
8

Using nditer() with multi_index option:

The nditer() function can be used with the multi_index option to access both the element and its indices during iteration.

import numpy as np

arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Iterate over each element with indices using nditer() and multi_index
with np.nditer(arr, flags=['multi_index']) as it:
while not it.finished:
index = it.multi_index
element = arr[index]
print(index, element)
it.iternext()

Output:

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

Using nditer() with ndenumerate():

You can combine the nditer() function with ndenumerate() to iterate over each element with its corresponding indices.

import numpy as np

arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Iterate over each element with indices using nditer() and ndenumerate
for index, element in np.ndenumerate(arr):
print(index, element)

Output:

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