1. Home
  2. Docs
  3. numpy
  4. Iteration use (np.nditer)

Iteration use (np.nditer)

1D Array Iteration using np.nditer:

import numpy as np

# Create a 1D array
arr_1d = np.array([1, 2, 3, 4, 5])

# Iterate through the 1D array using np.nditer
for element in np.nditer(arr_1d):
    print(element, end=' ')

2D Array Iteration using np.nditer:

import numpy as np

# Create a 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Iterate through the 2D array using np.nditer
for element in np.nditer(arr_2d):
    print(element, end=' ')

3D Array Iteration using np.nditer:

import numpy as np

# Create a 3D array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])

# Iterate through the 3D array using np.nditer
for element in np.nditer(arr_3d):
    print(element, end=' ')

How can we help?