Last modified: Jan 26, 2026 By Alexander Williams
Python List in List Append: Nested Data Guide
Python lists are versatile. They can hold any data type. This includes other lists. Creating a list within a list is called nesting. The append method is key for building these structures.
This article explains how to append lists to other lists. You will learn to create and manage nested lists. We cover practical examples and common pitfalls.
What is a Nested List?
A nested list is a list that contains other lists as its elements. It is also called a multi-dimensional list. Think of it as a container holding smaller containers.
This structure is perfect for representing grids, matrices, or hierarchical data. For example, a chessboard or student grades per class can be modeled this way.
# A simple nested list example
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix)
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Using Append to Create Nested Lists
The append method adds a single item to the end of a list. When you append a list, that entire list becomes one element. This is how you build a nested structure step-by-step.
# Start with an empty list
nested_list = []
# Create some simple lists
row1 = [1, 2, 3]
row2 = [4, 5, 6]
# Append the lists to create a nested structure
nested_list.append(row1)
nested_list.append(row2)
print("Nested List:", nested_list)
print("First inner list:", nested_list[0])
print("Second element of first list:", nested_list[0][1])
Nested List: [[1, 2, 3], [4, 5, 6]]
First inner list: [1, 2, 3]
Second element of first list: 2
Notice the double indexing. nested_list[0] gets the first inner list. nested_list[0][1] gets the second item inside that first list.
Appending vs. Extending: A Crucial Difference
Beginners often confuse append with extend. They serve different purposes. append adds its argument as a single element. extend adds each item from its argument individually.
This distinction is critical when working with lists. Using the wrong method gives unexpected results. For a deeper dive, see our guide on Python List Append vs Extend.
base_list = [10, 20, 30]
list_to_add = [40, 50]
# APPEND: Adds the entire list as one element
base_list.append(list_to_add)
print("After append:", base_list) # Output: [10, 20, 30, [40, 50]]
# Reset the list
base_list = [10, 20, 30]
# EXTEND: Adds each element individually
base_list.extend(list_to_add)
print("After extend:", base_list) # Output: [10, 20, 30, 40, 50]
After append: [10, 20, 30, [40, 50]]
After extend: [10, 20, 30, 40, 50]
Common Use Cases and Examples
Nested lists are useful in many scenarios. Let's explore some practical examples.
1. Building a Matrix or Grid
You can build a 2D grid dynamically. This is common in games or data analysis.
# Initialize an empty grid
grid = []
for i in range(3):
row = [] # Create a new row list
for j in range(3):
row.append(f"({i},{j})") # Fill the row
grid.append(row) # Append the completed row to the grid
print("2D Grid:")
for row in grid:
print(row)
2D Grid:
['(0,0)', '(0,1)', '(0,2)']
['(1,0)', '(1,1)', '(1,2)']
['(2,0)', '(2,1)', '(2,2)']
2. Storing Grouped Data
Store related data together. For instance, student scores in different subjects.
student_records = []
# Append lists of grades for each student
student_records.append(["Alice", 85, 92, 78])
student_records.append(["Bob", 88, 79, 95])
student_records.append(["Charlie", 70, 85, 88])
print("Student Records:", student_records)
# Access Bob's second grade
print("Bob's second grade:", student_records[1][2])
Student Records: [['Alice', 85, 92, 78], ['Bob', 88, 79, 95], ['Charlie', 70, 85, 88]]
Bob's second grade: 79
To perform calculations on this data, like finding a top score, techniques from Find Maximum in Python List can be very helpful.
Important Considerations and Pitfalls
Working with nested lists has some nuances. Being aware prevents bugs.
Reference vs. Copy
When you append a list, you append a reference to it, not a copy. Changing the original list later will affect the nested list.
inner_list = [1, 2, 3]
container = []
container.append(inner_list)
print("Before change:", container)
# Modify the original list
inner_list.append(4)
print("After change:", container) # The nested list is also updated!
Before change: [[1, 2, 3]]
After change: [[1, 2, 3, 4]]
To avoid this, append a copy of the list using .copy() or list(). Learn more in our article on Python List Append with Copy.
Handling Empty Lists and Errors
Appending to a non-existent list causes an AttributeError. Always initialize your list first. Similarly, incorrect indexing can lead to an IndexError or a ValueError.
Understanding these errors is crucial. Read about Understanding ValueError in Python List Operations to debug effectively.
Conclusion
Appending lists to create nested structures is a fundamental Python skill. The append method makes it simple to build matrices, grouped data, and complex hierarchies.
Remember the key points: append adds an entire list as one element, be mindful of references versus copies, and use indexing to access inner elements. This knowledge forms a strong foundation for working with more complex data structures in Python.
Practice with the examples provided. Experiment by building your own nested lists. Soon, handling multi-dimensional data will feel natural.