1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

Lists of lists

Lists can hold lists, which gives you a grid. The first index picks the row and the second picks the column:

board = [["X", "O", "X"],
         ["O", "X", " "],
         [" ", " ", "O"]]
board[0][2]     # row 0, col 2 -> "X"
board[1][2] = "X"
The Visualizer draws nested lists as rows, so you can see the grid.

See it run

row = [0, 0]
grid = [row, row]
grid[0][0] = 9
print(grid)
grid = [[0, 0], [0, 0]]
grid[0][0] = 9
print(grid)

Two grids, built differently. Follow the arrows: how many separate rows are there?

Practice lists of lists

Exercise 1

Use board in the editor below.

  • Place "X" in the bottom-left corner.
  • Store the top-right cell in top_right.
  • Store the whole middle row in middle.

Exercise 2

Use the cinema hall in the editor below: 1 is a taken seat and 0 is free. Using loops (or sum on each row):

  • taken = the total number of taken seats
  • row_free = a list with the number of free seats in each row

Exercise 3

Check the noughts-and-crosses board in the editor below for a win: - column1 = the list of cells at column index 1 (the middle column, one cell from each row) - x_wins = True if every cell in column1 is "X" - diagonal = the top-left to bottom-right diagonal: board[0][0], board[1][1], board[2][2] Use loops or comprehensions so this would work on any 3×3 board.

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest