What is Array Iteration?
Iterating over an array in NumPy refers to the process of visiting and accessing each element in the array one by one in a systematic manner. Iteration is used to perform operations on each element, such as calculations, value modifications, validations, or coordinate checks.
NumPy provides multiple flexible ways to iterate over arrays: standard Python for loops, multi-index tracking, flat iteration, and the advanced nditer() engine with custom memory-ordering and broadcasting support.
1. Using a Standard Python for Loop
In NumPy, you can use basic Python for loops to traverse through arrays. However, how the loop behaves depends directly on the dimensionality of the array.
A) Iterating Over 1-Dimensional Arrays
A 1-dimensional array is a linear list of elements. Iterating over it visits each scalar element sequentially:
Output:
B) Iterating Over Multi-Dimensional Arrays
When looping over multi-dimensional arrays with a basic for loop, NumPy iterates along the first axis (axis 0):
- In a 2D array (matrix), each iteration yields an entire row.
- In a 3D array, each iteration yields an entire 2D sub-matrix.
Example: Iterating Over a 2D Array (Row by Row)
Output:
Example: Iterating Over a 3D Array (Matrix by Matrix)
Output:
2. Iterating Over Elements in Multi-Dimensional Arrays
To reach every single scalar element in a multi-dimensional array using basic loops, you must use nested loops matching the number of dimensions.
Example: Accessing Every Element via Nested Loops
Output:
3. Iterating with Indices
Iterating over NumPy arrays using explicit index positions allows you to track and utilize coordinate locations (row, column) alongside the element values. This is essential when applying coordinate-dependent logic.
Example: Tracking Row & Column Coordinates
Output:
4. Using the numpy.nditer() Iterator Object
The nditer() function in NumPy provides an efficient multi-dimensional iterator object. Instead of writing deeply nested loops for 3D, 4D, or N-D arrays, nditer() flattens the traversal seamlessly through Python's standard iterator interface.
Example: Seamless Multi-Dimensional Traversal
Output:
5. Flat Iteration
Flat iteration refers to iterating through all elements of a multi-dimensional array as if it were a single flat sequence. With nditer, you can enable the flags=['buffered'] option to allow internal buffer allocation when casting types or managing memory buffers during traversal.
Example: Flat Traversal with Buffered Flag
Output:
6. Iteration Order: Row-Major vs Column-Major
Iteration order in NumPy refers to the sequence in which elements of an array are accessed during traversal:
- C-style (Row-Major, Default): Iteration progresses row by row. Iterates through all elements along the last dimension (columns) first.
- Fortran-style (Column-Major): Iteration progresses column by column. Iterates through rows first.
A) Default C-Style Order
Output:
B) Controlling Iteration Order with order='F'
By specifying order='F', nditer traverses column-wise down each column before moving to the next:
Output:
Why Column-Wise?
7. Broadcasting Iteration Across Multiple Arrays
Broadcasting iteration allows you to pass a sequence of multiple arrays to nditer() simultaneously. If the arrays have broadcast-compatible shapes, NumPy automatically broadcasts them together so you can process corresponding pairs or tuples of elements cleanly.
Example: Dual-Array Simultaneous Traversal
Output:
8. Vectorized Operations (Loops vs Vectorization)
While iterating with loops is essential for custom sequential procedures, vectorized operations in NumPy are heavily optimized in compiled C code and run significantly faster than manual Python loops.
Performance Tip: Always Vectorize When Possible
Whenever an operation can be expressed directly with array arithmetic (such as arr1 + arr2), prefer vectorization over explicit Python loops.
Example: Vectorized Addition
Output:
9. External Loop (flags=['external_loop'])
In NumPy, the external_loop flag modifies nditer() so that instead of returning single scalar values on every iteration, it returns 1-dimensional slices (chunks) of contiguous memory.
This dramatically reduces Python loop overhead by processing chunks of elements in batch!
Example: Chunk-based Traversal with External Loop
Output:
10. Modifying Array Values In-Place with op_flags=['readwrite']
By default, the nditer object treats arrays as read-only. If you attempt to assign to the iterator variable, NumPy will raise an error.
To modify array elements in-place during iteration, set op_flags=['readwrite'] and assign using the ellipsis notation: x[...] = new_value.
Example: Doubling Elements In-Place
Output:
x[...] = x * 2? Using x[...] ensures that you modify the memory referenced by the slice rather than re-binding the local Python variable x to a new scalar.
11. Summary of Iteration Techniques
| Method | Syntax | Key Characteristics |
|---|---|---|
Standard for loop |
for row in arr: |
Iterates along axis 0 (rows in 2D, slices in 3D). |
| Nested loops with indices | for i in range(r): for j in range(c): |
Provides explicit coordinates (i, j) for targeted manipulation. |
Basic nditer() |
for x in np.nditer(arr): |
Visits all elements in flat sequence without nested loops. |
Custom Order nditer() |
np.nditer(arr, order='F') |
Traverses column-by-column (Fortran column-major order). |
| Multi-Array Broadcasting | np.nditer([a, b]) |
Traverses multiple broadcast-compatible arrays simultaneously. |
| External Loop | flags=['external_loop'] |
Returns 1D contiguous slices for higher loop performance. |
| In-Place Modification | op_flags=['readwrite'] |
Allows modifying array values in place using x[...] = .... |
Array Iteration Quiz
Test your knowledge of array iteration in NumPy. Click an option to see instant feedback.