Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected
Python

Nested Lists in Python: A Practical Guide for Beginners

Learn how to create, access, and manipulate nested lists in Python with real-world examples like grade trackers and tic-tac-toe boards. This guide covers common operations, pitfalls like shallow copying, and when to use nested lists over other data structures.

July 2026 8 min read 1 views 0 hearts

When I first started learning Python, I thought lists were simple—just a collection of items. Then I discovered nested lists, and everything changed. Suddenly, I could represent tables, grids, and complex data structures with just a few lines of code. Let me show you how they work and why they're so useful.

What Exactly Is a Nested List?

A nested list is simply a list that contains other lists as its elements. Think of it like a filing cabinet where each drawer holds its own set of folders. Here's the basic structure:

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

This creates a 3x3 grid. The outer list has three items, and each item is itself a list containing three numbers. You can visualize it as:

Row 0: [1, 2, 3]
Row 1: [4, 5, 6]
Row 2: [7, 8, 9]

Why Would You Use Nested Lists?

Let me give you a real-world example from PythonSkillset. Imagine you're building a simple grade tracker for a classroom. Each student has multiple test scores. A nested list is perfect for this:

grades = [
    ["Alice", 85, 92, 78],
    ["Bob", 70, 88, 91],
    ["Charlie", 95, 89, 84]
]

Now you can access Alice's second test score with grades[0][2] (which gives 78). The first index picks the student, the second picks the test.

How to Create and Access Nested Lists

Creating a nested list is straightforward. You just put lists inside square brackets:

# A 2x3 matrix
matrix = [[1, 2, 3], [4, 5, 6]]

To access elements, use multiple indices. The first index selects the inner list, the second selects the element within that list:

print(matrix[0][1])  # Output: 2
print(matrix[1][2])  # Output: 6

You can also modify elements the same way:

matrix[0][0] = 10
print(matrix)  # Output: [[10, 2, 3], [4, 5, 6]]

Real-World Example: Student Grades

Let's build something practical. At PythonSkillset, we often help beginners track data. Here's a grade book using nested lists:

students = [
    ["Alice", 85, 92, 78],
    ["Bob", 70, 88, 91],
    ["Charlie", 95, 89, 84]
]

# Calculate average for each student
for student in students:
    name = student[0]
    grades = student[1:]
    average = sum(grades) / len(grades)
    print(f"{name}'s average: {average:.1f}")

This outputs:

Alice's average: 85.0
Bob's average: 83.0
Charlie's average: 89.3

Common Operations on Nested Lists

Iterating through all elements requires nested loops:

for row in matrix:
    for item in row:
        print(item, end=" ")
    print()  # New line after each row

Finding the length of a nested list works differently. len(matrix) gives the number of rows, not total elements. To get the total count, you need to sum the lengths of each inner list:

total_elements = sum(len(row) for row in matrix)

Adding new rows is as simple as appending a list:

matrix.append([10, 11, 12])

Removing a row uses the same methods as regular lists:

del matrix[1]  # Removes the second row

Common Pitfalls to Avoid

Shallow copying is a classic mistake. If you copy a nested list with copy() or slicing, you only copy the outer list. The inner lists are still shared:

original = [[1, 2], [3, 4]]
shallow_copy = original.copy()
shallow_copy[0][0] = 99
print(original[0][0])  # Output: 99 (unexpected!)

To make a true deep copy, use copy.deepcopy():

import copy
deep_copy = copy.deepcopy(original)
deep_copy[0][0] = 99
print(original[0][0])  # Output: 1 (safe!)

Practical Example: Tic-Tac-Toe Board

Let's build a simple tic-tac-toe board using nested lists. This is a classic beginner project at PythonSkillset:

board = [
    [" ", " ", " "],
    [" ", " ", " "],
    [" ", " ", " "]
]

# Make a move
board[0][0] = "X"
board[1][1] = "O"
board[2][2] = "X"

# Display the board
for row in board:
    print(" | ".join(row))
    print("-" * 9)

Output:

X |   |  
----------
  | O |  
----------
  |   | X

When to Use Nested Lists vs Other Structures

Nested lists are great for: - Grids and matrices (like game boards or image pixels) - Tabular data where rows have the same structure - Hierarchical data with fixed depth

But for more complex data, consider dictionaries or pandas DataFrames. Nested lists can get confusing with more than two levels of nesting.

A Quick Tip for Beginners

When debugging nested lists, print them one row at a time:

for row in my_nested_list:
    print(row)

This makes the structure much clearer than printing the whole thing at once.

Nested lists are one of those Python features that seem simple but open up so many possibilities. Once you get comfortable with them, you'll find yourself using them everywhere—from game development to data analysis. Give them a try in your next project at PythonSkillset, and you'll see how powerful they can be.

Comments

Questions, corrections, and tips stay visible for everyone reading this page.

0 in thread

Join the discussion

Shown next to your comment.

Up to 4,000 characters

No comments yet

Be the first to leave a note — it helps the next reader.