Put containers inside containers: 2D grids addressed by [row][column], and real-world records as lists of dictionaries.
01
Learning objectives
Access any cell of a 2D list using two indexes, grid[row][column].
Trace nested loops visiting every cell of a grid in row-major order.
Explain the difference between grid[1] and grid[1][2].
Store records as a list of dictionaries and read fields by key.
Write the loop that finds the record with the maximum value of a field.
02
Lesson notes
Real data nests: a seating plan is rows of seats, a spreadsheet is rows of cells, a marksheet is many students each with several fields. Python models all of these by putting containers inside containers. A 2D list is a list whose elements are themselves lists — one inner list per row. A set of records is the opposite arrangement: a list of dictionaries, one dictionary per student, each mapping field names to values.
Address a grid cell with two indexes in order: grid[row][column]. In grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], grid[1] is the whole second row [4, 5, 6], and grid[1][2] drills into that row to reach 6. Read it outside-in: first choose the row, then the position inside that row. Mixing the order — column first — is the single most common exam mistake in this topic.
Visiting every cell takes a loop inside a loop. The outer loop walks along the rows; for each row, the inner loop walks along that row's values. This visiting order — row by row, left to right — is called row-major order. The same skeleton totals a grid, searches a grid or prints it as a table; only the body of the inner loop changes between those jobs.
For mixed data, a list of dictionaries beats a grid of positions: students = [{'name': 'Ayesha', 'marks': 88}, ...]. Each record reads like a labelled row of a spreadsheet — s['name'] needs no memory of which column held the name, and adding a new field later breaks nothing. Loop over the list to process each record, and compare one field to find extremes, exactly as you would compare plain numbers.
The find-the-best-record pattern appears in every board paper: start with best = students[0], loop through the rest, and whenever s['marks'] > best['marks'] rebind best = s. Afterwards best holds the whole winning record, so both its name and its marks are one lookup away. Notice that you compare a single field but store the entire dictionary — a detail worth a mark in itself.
03
Worked code examples
Two indexes and a nested loop
grid[1] selects the second row and [2] the third value inside it — 6. The nested loops visit all nine cells, and 1 through 9 sums to 45.
grid = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(grid[1][2])
total = 0
for row in grid:
for value in row:
total = total + value
print(total)
Output
6
45
Records as a list of dictionaries
Each loop reads fields by key instead of remembering column positions. The max pattern keeps the whole best record, so the name follows from the marks.
students = [
{"name": "Ayesha", "marks": 88},
{"name": "Bilal", "marks": 72},
{"name": "Sana", "marks": 95},
]
for s in students:
print(s["name"], s["marks"])
best = students[0]
for s in students:
if s["marks"] > best["marks"]:
best = s
print("Top:", best["name"])
Output
Ayesha 88
Bilal 72
Sana 95
Top: Sana
Per-row summaries of a grid
Each pass of the outer loop hands one whole inner list to max() and min() — no inner loop is needed when a built-in already does the work.
grid = [[3, 8, 1],
[9, 2, 5]]
for row in grid:
print(max(row), min(row))
Output
8 1
9 2
04
Key terminology
2D list
A list whose elements are lists — a grid with rows and columns, addressed as grid[r][c].
nested loop
A loop written inside another loop's body; the inner one finishes completely for every single outer step.
row-major order
Visiting grid cells row by row, left to right — the standard nested-loop order.
record
One complete set of fields about a single entity, stored as a dictionary in a list of records.
field
One named attribute of a record, read by key, e.g. s['marks'].
05
Common misconceptions
Common mistake
grid[1][2] means row 2, column 1.
Correct understanding
Indexes read outside-in: grid[1] picks row 1 (the second row) first, then [2] picks its third element. The row comes first.
Common mistake
A list of dictionaries is just another way to write a 2D list.
Correct understanding
A grid stores values by numeric position; a list of dicts stores labelled fields by key. Grids suit uniform numbers, records suit mixed data.
Common mistake
The nested-loop total only works for square grids.
Correct understanding
It works for any shape — each inner loop simply runs as many times as that row is long, including uneven rows.
06
Check your understanding
1. grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. What is grid[2][0]?
2. A 2D list has 3 rows and 4 columns. How many times does the body of the inner loop run in a full nested traversal?
3. records = [{'name': 'Ali', 'city': 'Lahore'}, {'name': 'Umar', 'city': 'Multan'}]. Which line prints Multan?