Iterating Over Elements of a NumPy Array
NumPy Array - Iterate Over Elements
To iterate over elements of a NumPy array, you can use the numpy.nditer
iterator object.
numpy.nditer
provides Python’s standard iterator interface to visit each element in the NumPy array. You can use a for loop to traverse the elements of this iterator object.
Note: NumPy arrays with any number of dimensions can be iterated using numpy.nditer
.
Examples
1. Iterate Over Elements of a 2D NumPy Array
In the following example, we have a 2D array, and we use numpy.nditer
to print all the elements of the array.
Python Program
import numpy as np
# 2D array
a = (np.arange(8) * 2).reshape(2, 4)
# Print the array
print("The array:\n", a)
print("\nIterating over all the elements of the array:")
# Iterate over elements of the array
for x in np.nditer(a):
print(x, end=' ')
Explanation:
- We import the
numpy
library asnp
. - We create a 2D array
a
usingnp.arange(8)
multiplied by 2 and reshape it to dimensions (2, 4). - We print the array to visualize its structure.
- Using
numpy.nditer
, we iterate over all the elements of the array and print them in a single line.
Output:
The array:
[[ 0 2 4 6]
[ 8 10 12 14]]
Iterating over all the elements of the array:
0 2 4 6 8 10 12 14
2. Iterate Over Elements of a 3D NumPy Array
In this example, we demonstrate how to iterate through a 3D NumPy array.
Python Program
import numpy as np
# 3D array
a = np.arange(27).reshape(3, 3, 3)
# Print the array
print("The 3D array:\n", a)
print("\nIterating over all the elements of the 3D array:")
# Iterate over elements of the array
for x in np.nditer(a):
print(x, end=' ')
Explanation:
- We create a 3D array
a
with dimensions (3, 3, 3) usingnp.arange(27)
. - We print the 3D array to see its structure.
- Using
numpy.nditer
, we iterate over all the elements in the 3D array, regardless of its dimensionality.
Output:
The 3D array:
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
Iterating over all the elements of the 3D array:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
3. Iterate Over a NumPy Array with a Custom Order
In this example, we demonstrate how to iterate through a 2D array in a column-major order (F-style).
Python Program
import numpy as np
# 2D array
a = (np.arange(8) * 2).reshape(2, 4)
# Print the array
print("The array:\n", a)
print("\nIterating over the elements in column-major order:")
# Iterate over elements in column-major order
for x in np.nditer(a, order='F'):
print(x, end=' ')
Explanation:
- We create a 2D array
a
with dimensions (2, 4). - We print the array to observe its layout.
- Using
numpy.nditer
with theorder='F'
parameter, we iterate over the elements in column-major (Fortran-style) order instead of the default row-major (C-style) order.
Output:
The array:
[[ 0 2 4 6]
[ 8 10 12 14]]
Iterating over the elements in column-major order:
0 8 2 10 4 12 6 14
Summary
In this NumPy Tutorial, we learned how to iterate over the elements of a NumPy array using numpy.nditer
, covering:
- Iterating over a 2D array.
- Iterating over a 3D array.
- Iterating with a custom order (column-major).
Using numpy.nditer
, you can efficiently traverse arrays of any dimensionality and customize the iteration order as needed.