Skip to main content

How to Create a List of Lists in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Rows and columns, such as seats in a cinema or cells in a table, fit naturally into a list whose items are lists themselves. This guide builds one in Python 3.15.2 and reads a row, a single item, and the number of rows and columns.

Syntax​

Syntax
<name> = [[<item>, <item>], [<item>, <item>]]
<name>[<row>]
<name>[<row>][<column>]
PartWhat it means
[[...], [...]]An outer list whose items are lists: one inner list per row
<name>[<row>]One whole row, counting from 0
<name>[<row>][<column>]One item: the row first, then the position inside that row

The Python tutorial says: "It is possible to nest lists (create lists containing other lists)".

The Example​

A small cinema has two rows of three seats, A1 to A3 and B1 to B3. The script prints the whole plan, the first row, the last seat of the second row, and the number of rows and seats per row.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

grid.py
seats = [["A1", "A2", "A3"], ["B1", "B2", "B3"]]

print(seats)
print(seats[0])
print(seats[1][2])
print(len(seats), len(seats[0]))

The grid.py script in a macOS-style code window: seats is a list of two lists, A1 to A3 and B1 to B3; the script prints seats, seats[0], seats[1][2], and len(seats) with len(seats[0]).

Step 2: Run It​

Terminal
uv run python grid.py
Output
[['A1', 'A2', 'A3'], ['B1', 'B2', 'B3']]
['A1', 'A2', 'A3']
B3
2 3

The terminal after uv run python grid.py: the nested list, then the first row A1, A2, A3, then B3, then 2 3.

  • seats[0] is the first inner list, the whole A row.
  • seats[1][2] takes the second row ([1]), then its third seat ([2]): B3. Positions start at 0.
  • len(seats) counts the rows, 2, and len(seats[0]) counts the seats in the first row, 3.

The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Building rows with [[0] * 3] * 2Both rows are the same list: after rows[0][0] = 9, it printed [[9, 0, 0], [9, 0, 0]][[0] * 3 for _ in range(2)] makes a new list per row: it printed [[9, 0, 0], [0, 0, 0]]
Swapping the indexes, seats[2][1]IndexError: list index out of range: there is no third rowRow first, then column
Rows of different lengthslen(seats[0]) no longer tells you every row's lengthKeep the rows the same length when they stand for a grid

FAQ​

Can the inner lists hold different types?​

Yes. Each inner list is an ordinary list, so it can hold numbers, text or anything else.

How do I add a row?​

Append a list to the outer list: seats.append(["C1", "C2", "C3"]).

Conclusion​

seats = [["A1", "A2", "A3"], ["B1", "B2", "B3"]] held two rows. seats[0] gave the first row, seats[1][2] gave B3, and len() gave 2 rows of 3 seats.

Additional Resources​