A nested list is a list that contains other lists as its elements. While a flat list holds values like numbers or strings in a single row, a nested list arranges data in two or more dimensions, forming a grid, a matrix, or a tree-like branching structure. The most common beginner use case is a two-dimensional grid where each inner list represents a row and each element inside that row represents a column. Nested lists are Python's built-in way to handle multi-dimensional data without importing any library, and they appear in programs that process spreadsheets, game boards, pixel images, timetable schedules, and any data that naturally fits into rows and columns.
If you have already read about creating and accessing Python lists, you know that a list element can be any Python object, including another list. That single fact is what makes nesting possible. You build a nested list the same way you build a flat list, but instead of putting numbers or strings between the commas, you put entire lists. The skills from that article, indexing, slicing, and accessing items by position, apply unchanged at every level of the nested structure.
Creating nested lists correctly
The most straightforward way to create a small nested list is to write it as a literal, with each inner list on its own line for readability.
grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]This grid has three rows and three columns. The outer list contains three elements, and each of those elements is itself a list of three numbers. You can verify the structure by checking the length of the outer list, which returns 3, and the length of any inner list, which also returns 3.
For larger grids, writing every element by hand is impractical. A list comprehension creates each inner list programmatically, ensuring that each row is a separate, independent list object. This is the critical detail: every inner list must be created fresh. A common beginner mistake is to use the multiplication operator on a list of lists, which creates multiple references to the same inner list object.
grid = [[0 for col in range(4)] for row in range(3)]This comprehension creates three independent inner lists, each containing four zeros. Modifying grid[0][1] changes only the element in the first row and second column, leaving the other rows untouched. The alternative using the multiplication operator, [[0] * 4] * 3, would create three references to a single inner list. Changing one row would silently change all rows because they all point to the same object. The article on copying Python lists explains why this aliasing behavior happens and how to avoid it.
Accessing elements in a nested list
Accessing an element in a nested list uses chained square-bracket indexing. The first index selects the inner list, and the second index selects the element inside that inner list. This row-then-column convention is called row-major order and is the standard in Python and most programming languages.
To reach the element in the second row and third column of the grid above, you write grid[1][2]. The index 1 selects the second inner list, which is [4, 5, 6], and the index 2 selects the third element of that inner list, which is 6. Negative indices work at both levels: grid[-1][-1] gives you the bottom-right element regardless of the grid's dimensions.
You can also slice nested lists, though the result is still nested. Writing grid[0:2] gives you a new list containing the first two inner lists. Those inner lists are the same objects as the originals, so modifying an element through the sliced view also modifies the original grid. This is the shallow copy behavior from the copying article playing out at the slice level.
Iterating over nested lists
The natural way to visit every element in a nested list is with nested for loops. The outer loop walks through each inner list, and the inner loop walks through each element inside that inner list. This pattern processes every value in row-major order and works for grids of any dimensions.
A nested list comprehension flattens a two-dimensional structure into a single flat list. Placing two for clauses inside a single comprehension iterates over every element in column-major fashion, producing a one-dimensional list of all values. This is useful when you need to search for a value, compute a statistic across the entire grid, or transform the data into a format that a function expecting a flat list can accept.
Nested lists and shallow copies
The interaction between nested lists and copying is one of the most important concepts to internalize early. A shallow copy of a nested list, whether made with slicing, the copy method, or the list constructor, only duplicates the outer list. The inner lists are shared between the original and the copy. Modifying an inner list through the copy changes it for the original as well.
This is why the deepcopy function from the copy module is necessary for nested data. A deep copy recursively duplicates every level of nesting, creating independent inner lists all the way down. For a nested list that will be modified independently from its original, a deep copy is the only safe choice.
The article on list comprehensions in Python covers the comprehension syntax in depth, including nested comprehensions that can transform, filter, and flatten multi-dimensional data in a single readable expression. Together, nested lists and comprehensions give you a toolkit for handling structured data that rivals the convenience of spreadsheet formulas, all within pure Python.
Rune AI
Key Insights
- A nested list is a list whose elements are themselves lists, forming a row-and-column structure.
- Access elements with chained indexing: grid[row][col].
- Create nested lists with comprehensions, not with the multiplication operator, to avoid shared inner lists.
- Shallow copies only duplicate the outer list; inner lists remain shared with the original.
- Nested for loops and nested comprehensions are the standard tools for iterating over multi-dimensional lists.
Frequently Asked Questions
How do I create a 3x3 grid of zeros in Python?
How do I access elements in a nested list?
How do I iterate over every element in a nested list?
Conclusion
Nested lists are Python's built-in way to represent grids, matrices, and multi-dimensional data without importing any library. Mastering row-major indexing, proper creation with comprehensions to avoid shared inner lists, and the interaction between nesting and shallow copies prepares you for more advanced data structures.
More in this topic
List Comprehensions in Python
Master Python list comprehensions to create, filter, and transform lists in a single line. Learn the syntax, common patterns, and when to use a regular loop instead.
Copy Python Lists
Learn how to copy Python lists using slicing, the copy method, the list constructor, and the copy module. Understand shallow vs deep copies.
Working with Python Tuples
Learn what Python tuples are, how they differ from lists, when to use them, and why immutability makes tuples the right choice for fixed collections of data.