Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

How to Iterate Through a 2D Array in Python (Step-by-Step)

Use nested loops to visit every value in a Python 2D list or NumPy array. Add enumerate() for coordinates, or use NumPy’s .flat for a flat traversal.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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:

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
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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 matrix is clearer than indexing with range(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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.