Python 2D Lists and Grids for Board Games

How to make and use 2D lists (grids) in Python for board games: create rows safely, read grid[row][col], check neighbours, and print a board.

Board games need a grid: rows and columns. In Python a grid is a list of lists: a list of rows, where each row is a list of cells. grid[row][col] reads one cell.

example 1 · press Run, then edit and run again

Checking neighbours

Minesweeper-style games look at the 8 cells around a square. Loop over -1, 0 and 1 in both directions, skip the cell itself, and stay inside the edges.

example 2 · press Run, then edit and run again

Rows, columns and boxes

💡 [["."] * 4] * 3 looks right but every row is the same list, so changing one cell changes a whole column.

Games that use 2D lists and grids

Practise this with 5 of the 20 games:

Frequently asked questions

How do you make a 2D list in Python?

Use a list comprehension: [[0 for _ in range(cols)] for _ in range(rows)]. Each row is then its own list.

Is it grid[x][y] or grid[y][x]?

With a list of rows it's grid[row][col], which means grid[y][x]. Mixing them up is the most common grid bug.

More Python concepts