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:

import numpy as np # Create a 1-dimensional NumPy array arr = np.array([1, 2, 3, 4, 5]) # Iterate over the array for element in arr: print(element)

Output:

1 2 3 4 5

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)
import numpy as np # Create a 2D NumPy array (3x3 matrix) arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Iterate over the 2D array for row in arr_2d: print(row)

Output:

[1 2 3] [4 5 6] [7 8 9]
Example: Iterating Over a 3D Array (Matrix by Matrix)
import numpy as np # Create a 3D NumPy array (2x2x3) arr_3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) # Iterate over the 3D array for matrix in arr_3d: print(matrix)

Output:

[[1 2 3] [4 5 6]] [[ 7 8 9] [10 11 12]]

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

import numpy as np # Create a 2-dimensional NumPy array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Iterate over rows (i) and columns (j) for i in range(arr_2d.shape[0]): for j in range(arr_2d.shape[1]): print(arr_2d[i, j])

Output:

1 2 3 4 5 6 7 8 9

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

import numpy as np # Create a 2-dimensional NumPy array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Get the dimensions of the array rows, cols = arr_2d.shape # Iterate over the array using coordinates for i in range(rows): for j in range(cols): print(f"Element at ({i}, {j}): {arr_2d[i, j]}")

Output:

Element at (0, 0): 1 Element at (0, 1): 2 Element at (0, 2): 3 Element at (1, 0): 4 Element at (1, 1): 5 Element at (1, 2): 6 Element at (2, 0): 7 Element at (2, 1): 8 Element at (2, 2): 9

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

import numpy as np # Example 2D array arr = np.array([[1, 2], [3, 4]]) # Iterate using nditer (visits every element without nested loops) for x in np.nditer(arr): print(x)

Output:

1 2 3 4

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

import numpy as np # Create a 2D array arr = np.array([[1, 2, 3], [4, 5, 6]]) # Flat iteration using nditer with 'buffered' flag print("Iterating over the array:") for x in np.nditer(arr, flags=['buffered']): print(x, end=' ')

Output:

Iterating over the array: 1 2 3 4 5 6

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

import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print("Default Iteration Order (C-style, row-major):") for x in np.nditer(arr): print(x, end=' ')

Output:

Default Iteration Order (C-style, row-major): 1 2 3 4 5 6

B) Controlling Iteration Order with order='F'

By specifying order='F', nditer traverses column-wise down each column before moving to the next:

import numpy as np arr = np.array([[1, 2], [3, 4]]) # Iterate in Fortran-style order (column-major) for x in np.nditer(arr, order='F'): print(x)

Output:

1 3 2 4
Why Column-Wise?
Array: [1, 2] [3, 4] Column 0: 1, then 3 Column 1: 2, then 4 Output: 1 → 3 → 2 → 4

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

import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([10, 20, 30]) # Broadcasting addition operation print("Broadcasting Iteration:") for x, y in np.nditer([arr1, arr2]): print(x + y, end=' ')

Output:

Broadcasting Iteration: 11 22 33

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

import numpy as np arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([10, 20, 30, 40]) # Vectorized addition operation (No loops needed!) result = arr1 + arr2 print("Vectorized Operations:") print("Result of addition:", result)

Output:

Vectorized Operations: Result of addition: [11 22 33 44]

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

import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) # External loop iteration print("External Loop:") for chunk in np.nditer(arr, flags=['external_loop']): print(chunk)

Output:

External Loop: [1 2 3 4 5 6]

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

import numpy as np # Create a 1D array arr = np.array([1, 2, 3, 4, 5]) # Modify array elements in place with np.nditer(arr, flags=['buffered'], op_flags=['readwrite']) as it: for x in it: x[...] = x * 2 # In-place scalar update print("Modified Array Example:", arr)

Output:

Modified Array Example: [ 2 4 6 8 10]
Why 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.

1. When running for item in arr: on a 2D array of shape (3, 4), what does item represent on each iteration?
A) A single scalar element
B) An entire 1D row array of shape (4,)
C) An entire column of shape (3,)
D) A tuple containing (row_idx, col_idx)
2. What iteration order does np.nditer() use by default?
A) Row-major (C-style)
B) Column-major (Fortran-style)
C) Random order
D) Diagonal order
3. What flag must be set in nditer() to modify array elements in-place during traversal?
A) flags=['mutable']
B) flags=['write_mode']
C) op_flags=['readwrite']
D) order='W'
4. Given arr = np.array([[10, 20], [30, 40]]), what will for x in np.nditer(arr, order='F'): print(x) print?
A) 10, 20, 30, 40
B) 10, 30, 20, 40
C) 40, 30, 20, 10
D) 20, 40, 10, 30
5. What is the main advantage of using flags=['external_loop']?
A) It converts 2D arrays into 3D arrays
B) It yields 1D contiguous slices rather than single scalar elements, improving performance
C) It reverses the iteration direction
D) It prevents broadcasting errors