Skip to content

2D List Practice

A list can hold a grocery run, a playlist, or a row of slot machine symbols — anything that fits in a single line. Some data refuses to stay in one line: a checkerboard, a spreadsheet, the seating chart at a theater. Today you'll meet the container that fits, and you'll break it on purpose to see what happens.


Overview

In this lab you'll learn what a data structure is, meet the 2D list, and work through six short challenges in a starter file: reading values out of a grid, changing them, looping over all of them, and running one index deliberately off the edge to see how Python complains.

Builds on: Slot Machine — you'll reuse creating a list and reading items by index.

Before you start

Download the starter file and open it in your Python editor. All six challenges live in this one file.

Download 2d_list_practice.py

You've got it when…

  • You can print any single letter from the board using its row and column.
  • You can change a letter on the board with one assignment.
  • Your nested loop prints the board column by column.
  • You've caused an IndexError on purpose and can explain why it happened.

Collaboration & AI

Work: On your own. Compare answers with a neighbor after you've made your own predictions.

AI — AIAS Level 1, No AI: Indexing has to live in your fingers before the next two labs, so write these yourself. What the levels mean.


What Is a Data Structure?

A data structure is a way of organizing data in a program so that it can be stored and found on purpose, not by accident. You already know one: the list. A list is like one row of theater seats — every seat is numbered, the numbers start at 0, and one number finds any seat in the row.

A 2D list is the whole theater. To find a seat, you need two numbers: the row, then the seat within that row. In Python, a 2D list is nothing more exotic than a list whose elements are themselves lists — a list of rows.

The starter file builds this board, five rows of five letters:

2d_list_practice.py
board = [
    ["A", "B", "C", "D", "E"],
    ["F", "G", "H", "I", "J"],
    ["K", "L", "M", "N", "O"],
    ["P", "Q", "R", "S", "T"],
    ["U", "V", "W", "X", "Y"]
]

One index gets you a whole row. Two indexes get you a single letter:

print(board[1])     # the second row: ['F', 'G', 'H', 'I', 'J']
print(board[1][2])  # row 1, column 2: H

Warning

Counting starts at 0, in both directions. board[1] is the second row, and board[1][2] is the third letter in it. Row first, then column — always.

Why only 25 letters?

The alphabet has 26 letters and a 5 × 5 board seats 25. Z didn't make the cut. We'll fix that shortly.


Reading the Board

  1. Run the starter file as-is. Nothing prints — the board is built, and the program ends. That's expected; the challenges are yours to fill in.

  2. Complete Challenge 1: print the letter Q from the board, using one line only.

    Stuck? Open for a hint.

    Count rows from the top starting at 0, then count across that row starting at 0. Q sits in the fourth row — what index is that?

  3. Complete Challenge 2: print the letter M, then print the entire row that contains it, using two lines only.

    Run your program. Your screen should look like this.

    Q
    M
    ['K', 'L', 'M', 'N', 'O']
    

Looping Over the Board

Printing 25 letters with 25 print lines would work, and it would also be miserable. A nested loop — a loop inside another loop — visits every seat in the theater: the outer loop picks a row, and the inner loop walks the seats in it. This prints the board in reading order, A through Y:

for row in range(5):
    for col in range(5):
        print(board[row][col])
  1. Complete Challenge 3: write a nested loop that prints the board in column order — A, F, K, P, U, B, G, L, Q, V... — using three lines only.

    Stuck? Open for a hint.

    Compare the example loop to what you want. The example walks across each row; you want to walk down each column. Something small needs to swap.


Changing Letters

An index doesn't just read a value — it's also the address where a new value can be delivered. Assignment works on a 2D list exactly like it does on a 1D list:

board[0][1] = "!"   # the letter B is gone. It had a good run.
  1. Complete Challenge 4: Z has been waiting outside this entire lab. Write one assignment so Z takes Y's place on the board, then print the bottom row to prove it.

    Run your program. Your screen should look like this.

    ['U', 'V', 'W', 'X', 'Z']
    
  2. Complete Challenge 5: update all four corner letters to "*", then print the whole board with print(board) and check that exactly four letters changed.


Falling Off the Board

Every data structure has edges, and programs that index past them fail. An index that points outside the structure is called out of bounds — a ticket for row 5 in a theater whose rows stop at 4.

  1. Complete Challenge 6: read the commented-out line print(board[5][0]) and predict what it will do. Write your prediction down before you run anything.

  2. Remove the # and run the program. Your screen should look like this.

    Traceback (most recent call last):
      File "2d_list_practice.py", line 35, in <module>
        print(board[5][0])
    IndexError: list index out of range
    

    (Your line number may differ. The last line is the one that matters.)

  3. Answer the two questions in the comment: the largest row index that works, and the largest column index. Then put the # back so your finished program runs cleanly from top to bottom.

Reading the error

Python read board[5] and went looking for a sixth row that does not exist, so it stopped and raised an IndexError — it will not shrug and hand you a seat in the parking lot. board[0][5] fails the same way, one level deeper: the row exists, but the seat doesn't. A 5-row board's last valid index is 4, which is len(board) - 1.

Warning

Being off by exactly one — asking for index 5 in a list of length 5 — is one of the most common bugs in programming, with its own name: the off-by-one error. You will write one eventually. Now you'll recognize it.


Reference

Code What it does Example
board[r] The whole row r, as a list. board[2] → ['K', 'L', 'M', 'N', 'O']
board[r][c] The single value at row r, column c. board[3][1] → Q
board[r][c] = x Replaces the value at row r, column c. board[4][4] = "Z"
len(board) The number of rows. 5
len(board[0]) The number of columns in row 0. 5

Challenges

Try these on your own if you finish early.

  • The diagonal. Print the letters from the top-left corner to the bottom-right corner using one loop. Notice anything about the row and column numbers as you go?
  • A real grid. Print the board as five letters per line, so it looks like a board instead of a column. Hint: print accepts an end=" " argument that replaces the newline with a space.

Turn It In

  • Your completed 2d_list_practice.py file, with all six challenges done and your answers written in the Challenge 6 comment.

How It's Graded

This lab is worth up to 4 points. One score covers everything you turn in.

Score What it looks like
4 — Excellent All six challenges done within their line limits: Q and M printed by index, the column-order nested loop, Z seated in one assignment, exactly four corners starred — and Challenge 6 shows a written prediction made before you ran the crash, plus the correct largest row and column indexes in the comment. The # is back and the file runs clean.
3 — Above Average All six challenges work, with a slip — an extra line where one was allowed, a missed corner, or a prediction written but thin.
2 — Average About half the challenges done, or Challenge 6 run without the prediction and answers written down — the crash happened, the thinking didn't.
1 — Below Average The file runs but most challenges are untouched.
0 — Failing Nothing submitted, or the starter file exactly as you found it.