Accel Thinking

Python Learning Notes

How Do You Build and Traverse a 2D List?

Learn how to build, index, read, and traverse Python 2D lists while avoiding shared-row aliasing mistakes.

Maps, seating charts, score tables — many problems hand you a table instead of a single list. Beginners often get stuck here: the data clearly has rows and columns, but where does it go in the program? A 2D list is not mysterious at all: it is a “list that holds several rows,” and each row is itself a list. This article uses one small 2-by-3 example to show how to build one, read a cell, and visit every cell.

A Common Mistake

Look at this tempting shortcut first:

grid = [[0] * 3] * 2
grid[0][1] = 9
print(grid)

Many people expect [[0, 9, 0], [0, 0, 0]], but the real output is:

[[0, 9, 0], [0, 9, 0]]

We only changed the first row, so why did both rows change? Because [[0] * 3] * 2 copies not “two rows” but “two references to the same row object.” The two rows are the same row, so editing any cell shows up in both.

Two more mistakes happen when reading a table. Forgetting int(): without it, every cell stays a string, and addition turns into string joining. Swapping the indexes: grid[1][2] is not “row one, column two” in everyday counting — it is the row numbered 1 and the column numbered 2, both counted from 0. Remember the order: grid[row][col], row first, column second.

The Core Idea

A 2D list is a list of lists. Store this seating chart in a program:

col 0 col 1 col 2
row 0 4 7 2
row 1 9 1 5

It looks like this:

grid = [
    [4, 7, 2],
    [9, 1, 5],
]

Why two indexes? Because to find a person you first say which row they sit in (row), then which seat in that row (col). Read one cell with grid[row][col]: grid[1][2] is row 1, column 2, which is 5; grid[0][0] is the top-left corner, which is 4. For the whole table, len(grid) is the number of rows and len(grid[0]) is the number of columns.

In contests, a table usually comes from input: the first line is the number of rows r, the second line is the number of columns c, followed by r lines of c numbers each.

r = int(input())
c = int(input())
grid = []

for _ in range(r):
    row = [int(x) for x in input().split()]
    grid.append(row)

Read one line, convert to numbers, append one row — repeat r times and the grid is ready. If you prefer to create an empty table first and fill it later, the correct pattern is:

grid = [[0] * c for _ in range(r)]

The for _ in range(r) part makes every iteration produce a fresh list — that is the key, and it is exactly the fix for the shared-row trap above.

Visiting Every Cell

The outer loop controls rows, the inner loop controls columns, and together they visit each cell exactly once:

grid = [
    [4, 7, 2],
    [9, 1, 5],
]

for row in range(len(grid)):
    for col in range(len(grid[row])):
        print(row, col, grid[row][col])
0 0 4
0 1 7
0 2 2
1 0 9
1 1 1
1 2 5

Finish the three cells of row 0 first, then move into row 1 — left to right, top to bottom, like reading a book. Once traversal is in place, summaries follow. To print the sum of each row:

for row in grid:
    row_sum = 0
    for value in row:
        row_sum += value
    print(row_sum)
13
15

For column sums, do it the other way around: fix one column, then add it from top to bottom, giving 13, 8, 7. Watch where you reset the local row_sum — it must be cleared before starting each row, or the numbers merge into one running total.

Updating and Counting Cells

Updates and counts are just small variations of traversal. Add one to every cell:

for row in range(len(grid)):
    for col in range(len(grid[row])):
        grid[row][col] += 1

Counting “how many cells equal the target” is the same walk with a counter that increases on every match:

grid = [
    [4, 7, 2],
    [9, 1, 5],
]

target = 7
count = 0

for row in range(len(grid)):
    for col in range(len(grid[row])):
        if grid[row][col] == target:
            count += 1

print(count)
1

These are the same moves behind grid-style problems: a snail’s path across cells, harvest counts on a farm, or triangular fields.

Compare the Options

Pattern When to use it Watch out
[[0] * c] * r Looks quick All rows share one object; editing one cell changes every row. Do not use it.
[[0] * c for _ in range(r)] Build first, fill later Each row is independent. Recommended.
Read one row, then append it Contest input Remember int() for numeric cells, or they stay strings.
Fix row and vary col / fix col and vary row Row sums / column sums Reset the accumulator before the loop it belongs to.

Practice

  1. Basic: build [[4, 7, 2], [9, 1, 5]] and print grid[1][2] . (Answer: 5 )
  2. Variation: read r , c , and a numeric table, then print the sum of each row.
  3. Challenge: print the sum of each column, then the total of the whole table.

One-Sentence Takeaway

A 2D list is a list of lists: use grid[row][col], row before column, and make every row a fresh list.

If this article helped you sort out indexes and traversal, save it and practice along. To work on more grid-style problems, visit the Python Learning Notes channel on our website and follow the series step by step.