Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
ICSM Computer
12-Nov-20251. 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()