For a Python list of rows, use a nested loop: the outer loop selects each row, and the inner loop visits each value in that row. Add enumerate() at both levels when you also need row and column positions.
Iterate through every value in a 2D list
A Python 2D list is usually a list containing one list per row. Loop over the rows, then over each row’s values:
matrix = [
[1, 2, 3],
[4, 5, 6],
]
for row in matrix:
for value in row:
print(value)
The output is 1, 2, 3, 4, 5, then 6, each on its own line. This pattern follows the structure of the data: one loop for rows and one for the values inside each row. Python’s data structures tutorial also uses lists of lists to represent a matrix.
Get row and column positions as you loop
Use enumerate() for each loop when the coordinates matter. Python indexes start at zero, so the first row and first value have indices 0 and 0.
#1 Best Overall
for i, row in enumerate(matrix):
for j, value in enumerate(row):
print(i, j, value)
Here, i is the row index and j is the column index within that row. To retrieve a particular value, use matrix[i][j] for a nested list.
Handle rows that have different lengths
Nested loops work even when rows are ragged—that is, when they do not all contain the same number of values:
Rank #2
matrix = [
[1, 2],
[3, 4, 5],
]
for row in matrix:
for value in row:
print(value)
Each row is traversed according to its own length. Avoid using the first row’s width as a fixed column count unless you know every row has that many elements; otherwise, indexing a shorter row can raise an IndexError.
Choose the loop for your data and goal
| Data and goal | Pattern | What the loop yields |
|---|---|---|
| Nested Python list; visit each value | Nested for loops |
One value at a time, while keeping row structure in the code |
| Nested Python list; visit values with coordinates | Nested loops with enumerate() |
Row index, column index, and value |
| NumPy 2D array; visit each value by row | Nested for loops |
The outer loop yields rows; the inner loop yields the values in each row |
| NumPy array; visit all values as a flat stream | arr.flat |
Values in C-style order, without row grouping |
Iterate through a NumPy 2D array
A NumPy array is not the same type as a Python list of lists. For a 2D ndarray, one loop traverses the first axis and yields rows—not individual scalar values. Add an inner loop to visit each value:
Recommended Free Tools
for row in arr:
for value in row:
print(value)
NumPy describes full traversal of an N-dimensional array through nested loops, with one loop per dimension. See its array iterator documentation.
Flatten traversal with arr.flat
If you need a stream of every value rather than row-by-row grouping, iterate over arr.flat:
for value in arr.flat:
print(value)
NumPy documents .flat as traversing values in C-style order: the last index varies fastest. The yielded values do not retain their row grouping. For NumPy coordinates, a rectangular array’s element can be accessed as arr[i, j]. More details are in the NumPy indexing manual.
Use nditer when you need iterator controls
For basic traversal, nested loops or .flat are usually easier to read. NumPy’s nditer provides configurable multidimensional iteration, including multi-index tracking, for cases that need those controls. See NumPy’s iteration guide.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesQuick Recap
Best Value
Common mistakes and alternatives
- One loop over a NumPy 2D array: it visits rows. Nest another loop if you need each scalar value.
- Using indices when you do not need them:
for row in matrixis clearer than indexing withrange(len(matrix))when only values are needed. - Assuming every list row has the same length: loop over each row directly if the data might be ragged.
- Writing a Python loop for a whole-array transformation: with NumPy, check whether a vectorized operation expresses the transformation more clearly. The documentation cited here does not establish a performance comparison, so choose based on the operation rather than assuming a speed advantage.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




