Ad space (banner)
🐍Python Lessons
Lesson 25 / 69

Two-Dimensional Lists (grid-shaped data)

This lesson covers working with two-dimensional lists in Python, so you can represent table- or grid-shaped data. It's written for anyone searching "Python 2D list tutorial" or "Python nested list".

Putting a list inside a list lets you represent a "grid" with rows and columns — this is called a two-dimensional list. You access it with two indexes, like grid[row][column]. It's the basic idea behind representing a game board with coordinates, or spreadsheet-like data.

The sample code creates a 3×2 two-dimensional list called grid, and retrieves the value at row 2, column 3 with grid[1][2]. Using for row in grid: to pull out each row, and ",".join(map(str, row)) to display it neatly, is a common pattern when inspecting the contents of a 2D list.

A common beginner mistake is reversing the order of the row and column indexes. Writing grid[column][row] instead accesses a value at an unintended position. If you want more serious numerical computation, a dedicated library called NumPy is also commonly used — it's faster than 2D lists and comes with far more convenient features.

When the number of rows and columns isn't known ahead of time, you'll sometimes build the list up dynamically instead. Two-dimensional lists show up constantly in programs that manage grid-shaped data — image processing, game boards, and more.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)