How to Iterating NumPy Array?
Ask by ICSM Computer
Updated 12 Nov 2025
1. Basic Iteration (1-D Array)
If you have a 1-dimensional NumPy array, you can simply loop through it like a Python list:
Output:
2. Iterating a 2-D Array (Row by Row)
In a 2D array, iteration gives each row as a sub-array:
Output:
3. Iterating Each Element in a Multi-Dimensional Array
If you want to access every element (not just rows), use
np.nditer():Output:
4. Iterating with Data Type Conversion
You can convert elements while iterating, e.g. from
inttofloat:5. Iterating with Index (Using
np.ndenumerate)To access both index and value during iteration:
Output:
6. Iterating Along Specific Axis
You can iterate over a specific axis (like columns) using
np.apply_along_axis:Output:
Summary
for x in arr:np.nditer(arr)np.ndenumerate(arr)np.apply_along_axis()