Lesson 8: Nested Lists & 2D Data Tables
Lists inside lists: rows, columns and tabular data • Sections 9.3 (List Operations), 9.4 (Nested Lists), 5.3 (Nested Loops)
Save or Print Notes
Export this study guide as a clean PDF for exam preparation and quick revision.
1. What Is a Nested List?
A nested list is a list whose elements are themselves lists. Each inner list represents one row of data.
marks = [
[78, 92, 65], # row 0 (Ahmed)
[88, 74, 90], # row 1 (Fatima)
[95, 81, 70] # row 2 (Khalid)
]
print(marks[0]) # Output: [78, 92, 65] (a whole list!)
print(len(marks)) # Output: 3 (number of rows)
Core Mental Model:
marks[0] is not a number—it is the entire first inner list!
2. Rectangular versus Ragged Lists
Rectangular Lists (Standard)
Every inner list has the exact same length (e.g. 3x3 grid).
len(marks)reliably counts rows (3).len(marks[0])reliably counts columns (3).- Total items =
rows * cols = 9.
Ragged / Jagged Lists
Inner lists have different lengths.
ragged = [
[78, 92, 65],
[88, 74], # only 2 items!
[95, 81, 70]
]
⚠️ Danger: Ragged lists are legal Python, but code that assumes rectangular dimensions will stop with
IndexError: list index out of range when accessing ragged[1][2].
3. Two Subscripts: Row Then Column
Always read subscripts strictly left to right: marks[row][col].
- Pick the row:
marks[1]hands back Fatima's row[88, 74, 90]. - Pick the column item:
marks[1][2]extracts90.
| Expression | What Python Gives Back | Type |
|---|---|---|
marks |
The whole gradebook (3 inner lists) | list |
marks[0] |
[78, 92, 65] (Row 0) |
list |
marks[0][1] |
92 |
int |
marks[-1][-1] |
70 (bottom right element) |
int |
len(marks) |
3 (counts rows) |
int |
len(marks[0]) |
3 (counts columns) |
int |
Modifying a Value in Place:
To update Fatima's comp1001 score by +5:
To update Fatima's comp1001 score by +5:
marks[1][0] = marks[1][0] + 5 (new row: [93, 74, 90]).
4. Looping Over 2D Data
Element-Based Looping
Use when you only need the values, not the coordinates.
# Total each student:
for row in marks:
total = 0 # Resets for every student!
for value in row:
total = total + value
print(total)
# Output: 235, 252, 246
💡 Tip:
total = 0 sits inside the outer loop so every row starts fresh from zero.
Index-Based Looping
Use when you need the coordinates i and j, or need to mutate values.
for i in range(len(marks)):
for j in range(len(marks[i])):
print(f"Row {i}, Col {j}: {marks[i][j]}")
Totalling One Column (e.g. comp1001 at col 0):
col_total = 0
for i in range(len(marks)):
col_total += marks[i][0]
print(col_total) # 78 + 88 + 95 = 261
5. Building a Grid Trap & Flattening
The Repeated Grid Trap
# ❌ DANGEROUS: Stores SAME inner list twice!
grid = [[0] * 3] * 2
grid[0][0] = 9
print(grid)
# Output: [[9, 0, 0], [9, 0, 0]] (Both changed!)
# ✅ CORRECT: Builds a fresh row each time:
grid = []
for i in range(2):
grid.append([0] * 3)
grid[0][0] = 9
# Output: [[9, 0, 0], [0, 0, 0]]
Checking Shape & Flattening
Checking if Rectangular:
def is_rectangular(data):
if len(data) == 0:
return True
first_len = len(data[0])
for inner in data:
if len(inner) != first_len:
return False
return True
Flattening to 1D:
def flatten_list(data):
result = []
for row in data:
for val in row:
result.append(val)
return result