We can apply filtering and then proceed with iteration.
Iterate through each scalar element of the 2-D array, skipping every other element:
import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) for x in np.nditer(arr[:, ::2]): print(x) |
Enumeration refers to specifying the sequence number of items one by one.
In some cases, we need the corresponding index of each element during iteration; for these situations, the ndenumerate() method can be utilized.
Enumerate the elements of the following 1-D array:
import numpy as np arr = np.array([1, 2, 3]) for idx, x in np.ndenumerate(arr): print(idx, x) |
Enumerate the elements of the following 2-D 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) |