0Pricing
Python Academy · Lesson

Nested Lists and Iteration

Work with nested lists and iterate efficiently with for loops.

Nested Lists and Iteration is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Lists can contain other lists, creating multi-dimensional structures. Mastering nested iteration is key for grids and matrices.

Nested List Structure

matrix = [[1,2,3],[4,5,6],[7,8,9]] is a 3x3 grid. matrix[1][2] accesses row 1, column 2 — which is 6.
matrix = [[1,2,3],[4,5,6],[7,8,9]]
print(matrix[1][2])

Iterating Rows

for row in matrix: print(row) prints each sub-list. row is itself a list you can index.
matrix = [[1,2],[3,4]]
for row in matrix:
    print(row)

Nested for Loops

for row in matrix: for val in row: print(val) prints every individual element.
matrix = [[1,2],[3,4]]
for row in matrix:
    for val in row:
        print(val, end=' ')

Flatten a Nested List

[x for row in matrix for x in row] flattens one level. This is a nested list comprehension.
matrix = [[1,2],[3,4]]
flat = [x for row in matrix for x in row]
print(flat)

Building a Grid

Create an N×M grid: grid = [[0]*M for _ in range(N)] Use a list comprehension, NOT [[0]*M]*N which creates aliased rows.
grid = [[0]*3 for _ in range(3)]
grid[0][0] = 1
print(grid)

The Aliased Row Trap

grid = [[0]*3]*3 creates three references to the same row. Modifying one modifies all. Always use a comprehension.
bad = [[0]*3]*3
bad[0][0] = 9
print(bad)  # all rows changed!

zip() for Parallel Iteration

for a, b in zip(list1, list2) iterates two lists in parallel. zip() stops at the shorter list.
names = ['Alice', 'Bob']
scores = [90, 85]
for n, s in zip(names, scores):
    print(n, s)

enumerate() with Nested Lists

for i, row in enumerate(matrix): for j, val in enumerate(row): gives you both indices and values.
matrix = [[1,2],[3,4]]
for i, row in enumerate(matrix):
    for j, val in enumerate(row):
        print(f'[{i}][{j}]={val}')

Sorting a List of Lists

sorted(data, key=lambda x: x[1]) sorts by the second element of each sub-list.
data = [['Bob',85],['Alice',90],['Charlie',78]]
data.sort(key=lambda x: x[1])
print(data)

Transposing a Matrix

transposed = list(zip(*matrix)) transposes rows to columns. The * unpacks the outer list as arguments to zip.
matrix = [[1,2,3],[4,5,6]]
print(list(zip(*matrix)))

Quick Check

What is the correct way to create a 3x3 zero-filled grid to avoid the aliased row trap?

Recap

Nested lists: access with [row][col], iterate with nested for loops. Use comprehension to build grids. zip() for parallel iteration, zip(*matrix) to transpose.

Keep Going

Great work! Move on to the next lesson to continue building your skills.

Frequently asked questions

Is the “Nested Lists and Iteration” lesson free?

Yes — the full text of “Nested Lists and Iteration” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Nested Lists and Iteration”?

Work with nested lists and iterate efficiently with for loops. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Nested Lists and Iteration” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Creating and Accessing Lists
  2. Modifying Lists
  3. Tuples and Immutability
  4. Nested Lists and Iteration
← Back to Python Academy