Nested Lists: Lists Within Lists
When you first start coding, you quickly meet lists. A list is just a collection of items: numbers, words, or even other lists. That last part—lists inside other lists—is what this article is all about.
These are called nested lists. They may sound a bit scary at first, but they’re just a simple way to organize information in layers. By the end of this article, you’ll know how to create nested lists, access their items, change them, and use them in simple, real-world mini-projects.
We’ll use Python in the examples, because it’s beginner-friendly. Don’t worry if you’re new—everything will be explained step-by-step.
1. What Is a Nested List?
A regular list might look like this:
numbers = [1, 2, 3, 4]
A nested list is just a list where some items are lists themselves:
nested_numbers = [[1, 2], [3, 4]]
In nested_numbers, we have a list with two items:
- The first item is
[1, 2] - The second item is
[3, 4]
So you can think of a nested list as a list of lists.
Why is this useful? Because real-world data is often grouped:
- A list of students, where each student has [name, age]
- A tic-tac-toe board (3 rows, each row is a list)
- A weekly schedule (7 days, each day a list of tasks)
2. Creating Your First Nested List
Let’s start simple. Imagine you want to store information about two students. Each student has a name and an age.
Example 1: A list of students
# Each inner list holds [name, age]
student1 = ["Alice", 20]
student2 = ["Bob", 22]
# A nested list: a list of students
students = [student1, student2]
print(students)
What’s happening here?
student1is a list with 2 items: name and age.student2is the same type of list.studentsis a nested list that contains both students.
Expected output:
[['Alice', 20], ['Bob', 22]]
You now have a tiny “table” of data: 2 rows (students) and 2 columns (name, age).
Try it yourself
Change the code to add a third student, e.g. ["Carol", 19], and add it to students. Then print students again and see what changes.
3. Accessing Items in Nested Lists
To work with nested lists, you use two indexes:
- The first index chooses which inner list you want
- The second index chooses the item inside that inner list
Indexes start at 0 in Python.
Example 2: Getting specific values
We’ll reuse the students list.
students = [["Alice", 20], ["Bob", 22]]
# Get the first student (Alice's list)
first_student = students[0]
print(first_student) # ['Alice', 20]
# Get Alice's name
alice_name = students[0][0]
print(alice_name) # 'Alice'
# Get Bob's age
bob_age = students[1][1]
print(bob_age) # 22
How to read students[1][1]:
students[1]→ second inner list →['Bob', 22]students[1][1]→ second item in that list →22
Try it yourself
Add a third value to each student, like a favorite color, e.g. ['Alice', 20, 'blue']. Then practice:
- Print the color of each student
- Print just the second student’s color
This will help you get used to the two-level indexing.
4. Changing and Adding Items in Nested Lists
Nested lists are mutable, which means you can change them after you create them.
Changing an existing value
students = [["Alice", 20], ["Bob", 22]]
# Alice had a birthday! Update her age from 20 to 21
students[0][1] = 21
print(students) # [['Alice', 21], ['Bob', 22]]
We accessed students[0][1] (Alice’s age) and then assigned a new value.
Adding a new inner list
students = [["Alice", 21], ["Bob", 22]]
# Add a new student as a new inner list
new_student = ["Carol", 19]
students.append(new_student)
print(students)
Expected output:
[['Alice', 21], ['Bob', 22], ['Carol', 19]]
Now your nested list has three student records.
Try it yourself
- Start with an empty list:
students = [] - Use
append()to add three different student lists. - Print the final
studentslist.
This will help you see how nested lists grow over time.
5. Nested Lists and “Rows and Columns” (Like a Grid)
Nested lists are often used to represent grids or tables. Think of:
- Game boards
- Seating charts
- Simple spreadsheets
Let’s build a small 3x3 grid, like a tic-tac-toe board.
Example 3: A simple grid
# Each inner list is a row
board = [
[" ", " ", " "], # Row 0
[" ", " ", " "], # Row 1
[" ", " ", " "] # Row 2
]
print(board)
Right now, the grid is empty (just spaces). Let’s place an X in the middle.
# Place 'X' in the center: row 1, column 1
board[1][1] = "X"
print(board)
How to think about this:
board[1]→ second row:[" ", "X", " "](after the change)board[1][1]→ middle cell of that row
This is a powerful idea: nested lists can represent 2D structures.
Try it yourself
- Place an
Oin the top-left corner (row 0, column 0). - Place an
Xin the bottom-right corner (row 2, column 2). - Print
boardeach time to see the changes.
If you’re feeling brave, try printing each row on its own line using:
for row in board:
print(row)
6. Looping Through Nested Lists
You often want to go through every item in a nested list. You can do this with nested loops: a loop inside another loop.
Example 4: Printing every value nicely
board = [
["X", "O", " "],
[" ", "X", "O"],
["O", " ", "X"]
]
# Go through each row
for row in board:
# Go through each cell in the row
for cell in row:
print(cell, end=" ") # Print cell followed by a space, stay on same line
print() # Move to the next line after each row
What this does:
- The outer
for row in board:loop picks each inner list (each row). - The inner
for cell in row:loop picks each value in that row. print(cell, end=" ")prints cells in one line.- The empty
print()creates a line break between rows.
Expected output (one possible layout):
X O
X O
O X
It’s not pretty, but you can clearly see a 3x3 grid.
Try it yourself
Change the characters in board and run the loop again. Try different symbols or letters and see how the output changes.
If you’re ready for a small challenge, try adding a counter to count how many X values are on the board.
7. Common Beginner Mistakes (And How to Avoid Them)
Learning nested lists can feel confusing. Here are a few mistakes beginners often make:
Forgetting that indexing starts at 0
If you trystudents[2][0]but only have two students, you’ll get an error. Always check how many items you have.Mixing up rows and columns
With a grid, remember:grid[row][column]. The first index is the row, the second is the column.Trying to access a value that doesn’t exist yet
If an inner list has only 2 items,inner_list[2]will fail. Make sure the index you use is valid.
Don’t worry if you hit errors—they’re a normal part of learning. Read the error message, check your indexes, and try again.
8. Quick Recap and What’s Next
You’ve just learned:
- What a nested list is: a list that contains other lists.
- How to create nested lists to group related data.
- How to access items using two indexes, like
students[1][0]. - How to change and add items in nested lists.
- How to use nested lists to represent grids and how to loop through them.
These skills are a big step forward. Nested lists are the foundation for many more advanced topics, like matrices, game boards, and even basic data analysis.
What’s next?
- Practice building your own small grids (like a 2x2 or 4x4 board).
- Try representing real-world data: a weekly schedule or a list of products and prices.
- Later, you can explore related ideas like dictionaries, which store data in key–value pairs.
Every time you work with nested lists, you’re training your brain to think like a programmer. Keep experimenting, stay curious, and celebrate the progress you’ve already made—you’re doing great.
