In the previous lesson, you learned how to work with Python list indexing and slicing. While a normal list stores values in a single dimension, many real-world applications require storing data in rows and columns. For example, a school may store student names and marks, a company may maintain employee records, and a spreadsheet stores information in a tabular format.
Python solves this problem by allowing you to create nested lists, which are lists containing one or more lists as elements. Nested lists are also called two-dimensional (2D) lists when they represent rows and columns.
Nested lists are widely used in data analysis, game development, machine learning, scientific computing, and database applications.
In this lesson, you will learn what nested lists are, why they are useful, how to create them, access their elements, modify nested values, and apply them in real-world scenarios.
After completing this lesson, you will be able to:
A nested list is a list that contains one or more lists as its elements.
Instead of storing individual values, each element can itself be another list.
list_name = [
[row1],
[row2],
[row3]
]
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
print(students)
Output
[['Rahul', 85], ['Priya', 92], ['Amit', 78]]
Each inner list represents a student’s record.
Suppose you want to store student names and marks.
student1 = ["Rahul", 85]
student2 = ["Priya", 92]
student3 = ["Amit", 78]
Managing many separate lists becomes difficult.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
print(students)
Output
[['Rahul', 85], ['Priya', 92], ['Amit', 78]]
All related data is stored in a single structured list.
A nested list is often called a 2D list because it stores data in rows and columns.
| Row | Column 0 | Column 1 |
|---|---|---|
| 0 | Rahul | 85 |
| 1 | Priya | 92 |
| 2 | Amit | 78 |
Each inner list represents one row of data.
To access an element inside a nested list, use two indexes.
list_name[row][column]
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
print(students[0][0])
print(students[0][1])
Output
Rahul
85
The first index selects the row, and the second index selects the column.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
print(students[1][0])
print(students[2][1])
Output
Priya
78
Positive indexing begins from the first row and the first column.
matrix = [
[10,20],
[30,40]
]
print(matrix[1][0])
Output
30
Nested lists are mutable, so individual values can be changed.
students = [
["Rahul", 85],
["Priya", 92]
]
students[0][1] = 90
print(students)
Output
[['Rahul', 90], ['Priya', 92]]
The marks of Rahul are updated from 85 to 90.
Suppose a company stores employee information.
employees = [
[101, "Rahul", "Sales"],
[102, "Priya", "HR"],
[103, "Amit", "IT"]
]
print(employees[2][1])
print(employees[1][2])
Output
Amit
HR
Nested lists make it easy to organize structured records.
Consider the following program.
matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
print(matrix[2][1])
Execution Steps
2 selects the third row.1 selects the second column.8 is retrieved.Output
8
A single index returns an entire row instead of an individual value.
students = [
["Rahul",85],
["Priya",92]
]
print(students[0])
Output
['Rahul', 85]
The first index selects the row, while the second index selects the column.
Accessing a row or column that does not exist raises an IndexError.
Nested lists are mutable, so rows and values can be modified.
Python allows irregular nested lists, so always check the available columns.
In this section, you learned what Python nested lists are and why they are useful for storing structured data. You explored two-dimensional lists, created nested lists, accessed elements using row and column indexes, modified nested values, and applied these concepts to practical examples such as student and employee records. You also examined execution flow and common beginner mistakes. In the next section, you will learn how to traverse nested lists using loops, add and remove rows, update nested elements, calculate lengths, and work with nested lists more efficiently.
In the previous section, you learned how to create nested lists and access individual elements using row and column indexes. In real-world applications, however, you often need to process all the data stored in a nested list. Python makes this easy by allowing you to traverse nested lists using loops, add or remove rows, modify elements, and determine the size of nested structures.
These operations are commonly used in student management systems, inventory applications, spreadsheets, and data analysis projects.
Traversing means visiting each element of a nested list one by one. The most common approach is using nested for loops.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
for row in students:
for value in row:
print(value)
Output
Rahul
85
Priya
92
Amit
78
The outer loop selects each row, while the inner loop processes each value in that row.
If you only need each complete row, a single loop is sufficient.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
for row in students:
print(row)
Output
['Rahul', 85]
['Priya', 92]
['Amit', 78]
range() with Nested ListsYou can use indexes to access rows and columns explicitly.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
for i in range(len(students)):
print(students[i][0], students[i][1])
Output
Rahul 85
Priya 92
Amit 78
This approach is useful when row indexes are required.
The append() method adds an entire row to the nested list.
students = [
["Rahul", 85],
["Priya", 92]
]
students.append(["Neha", 89])
print(students)
Output
[['Rahul', 85], ['Priya', 92], ['Neha', 89]]
You can insert a new row at a specific position using insert().
students = [
["Rahul", 85],
["Amit", 78]
]
students.insert(1, ["Priya", 92])
print(students)
Output
[['Rahul', 85], ['Priya', 92], ['Amit', 78]]
Individual rows are normal Python lists, so list methods can also be used on them.
students = [
["Rahul", 85]
]
students[0].append("Delhi")
print(students)
Output
[['Rahul', 85, 'Delhi']]
The pop() method removes a row by its index.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
students.pop(1)
print(students)
Output
[['Rahul', 85], ['Amit', 78]]
You can remove values from an individual row.
students = [
["Rahul", 85, "Delhi"]
]
students[0].pop()
print(students)
Output
[['Rahul', 85]]
len() with Nested ListsThe len() function returns the number of rows in a nested list.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
print(len(students))
Output
3
You can also determine the number of columns in a row.
students = [
["Rahul", 85, "Delhi"]
]
print(len(students[0]))
Output
3
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
for student in students:
print("Name:", student[0])
print("Marks:", student[1])
print("-----------")
Output
Name: Rahul
Marks: 85
-----------
Name: Priya
Marks: 92
-----------
Name: Amit
Marks: 78
-----------
products = [
["Laptop", 15],
["Mouse", 40],
["Keyboard", 25]
]
products.append(["Monitor", 10])
for product in products:
print(product)
Output
['Laptop', 15]
['Mouse', 40]
['Keyboard', 25]
['Monitor', 10]
Consider the following program.
matrix = [
[1, 2],
[3, 4]
]
for row in matrix:
for value in row:
print(value)
Execution Steps
Output
1
2
3
4
A single loop accesses rows, not the individual elements inside each row.
To access a value inside a nested list, you need both a row index and a column index.
append() IncorrectlyAppending a normal value adds it as a row. To add a column value, append it to an inner list.
Always verify the row index before using pop() or del.
Nested lists can have rows with different lengths, so check the structure before accessing elements.
In this section, you learned how to work with nested lists by traversing them using loops, accessing values with indexes, adding and inserting rows, modifying individual rows, removing rows and elements, and using the len() function to determine the number of rows and columns. You also explored practical examples, execution flow, and common beginner mistakes. In the next section, you will learn advanced nested list concepts, including matrices, nested list comprehensions, shallow copy versus deep copy, best practices, performance tips, and real-world applications.
In the previous section, you learned how to traverse nested lists, add and remove rows, modify values, and work with nested data using loops. In this section, you will explore advanced concepts related to nested lists, including matrices, nested list comprehensions, copying nested lists, deep copy versus shallow copy, and practical applications.
These concepts are widely used in data science, machine learning, scientific computing, image processing, and software development.
A matrix is a two-dimensional arrangement of values organized into rows and columns. In Python, matrices are commonly represented using nested lists.
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix)
Output
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Each inner list represents one row of the matrix.
Matrix elements are accessed using row and column indexes.
matrix = [
[10, 20, 30],
[40, 50, 60],
[70, 80, 90]
]
print(matrix[1][2])
Output
60
The first index selects the row, while the second index selects the column.
Since nested lists are mutable, matrix values can be modified.
matrix = [
[10, 20],
[30, 40]
]
matrix[1][0] = 35
print(matrix)
Output
[[10, 20], [35, 40]]
Nested list comprehension creates or processes nested lists using a concise syntax.
matrix = [[0 for col in range(3)] for row in range(3)]
print(matrix)
Output
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
This program creates a 3 × 3 matrix filled with zeros.
Flattening converts a nested list into a one-dimensional list.
matrix = [
[1, 2],
[3, 4],
[5, 6]
]
flat = [item for row in matrix for item in row]
print(flat)
Output
[1, 2, 3, 4, 5, 6]
This technique is useful when preparing data for analysis.
Assigning one nested list to another variable does not create a new copy.
list1 = [
[1, 2],
[3, 4]
]
list2 = list1
list2[0][0] = 99
print(list1)
Output
[[99, 2], [3, 4]]
Both variables refer to the same nested list.
A shallow copy creates a new outer list, but the inner lists are still shared.
import copy
list1 = [
[1, 2],
[3, 4]
]
list2 = copy.copy(list1)
list2[0][0] = 99
print(list1)
Output
[[99, 2], [3, 4]]
The inner lists remain shared, so changes affect both variables.
A deep copy creates completely independent copies of both the outer list and all inner lists.
import copy
list1 = [
[1, 2],
[3, 4]
]
list2 = copy.deepcopy(list1)
list2[0][0] = 99
print(list1)
print(list2)
Output
[[1, 2], [3, 4]]
[[99, 2], [3, 4]]
The original nested list remains unchanged.
students = [
["Rahul", 85],
["Priya", 92],
["Amit", 78]
]
for student in students:
print(student[0], ":", student[1])
Output
Rahul : 85
Priya : 92
Amit : 78
seats = [
["A1", "A2", "A3"],
["B1", "B2", "B3"],
["C1", "C2", "C3"]
]
print(seats[2][1])
Output
C2
Nested lists are useful for representing classroom seating arrangements.
Consider the following program.
matrix = [
[2, 4],
[6, 8]
]
for row in matrix:
for value in row:
print(value)
Execution Steps
Output
2
4
6
8
matrix, students, or employees.copy.deepcopy() when independent copies are required.Shallow copies share inner lists, while deep copies create completely independent copies.
Nested lists require both row and column indexes.
Python allows rows of different sizes.
Assigning one nested list to another variable creates another reference, not a new copy.
Always maintain correct indentation for nested loops to avoid logical errors.
In this section, you learned advanced nested list concepts including matrices, nested list comprehensions, flattening nested lists, shallow copy versus deep copy, and practical applications such as student records and seating arrangements. You also explored best practices, performance tips, execution flow, and common beginner mistakes. In the final section, you will review the complete lesson with a lesson summary, key takeaways, frequently asked questions, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python List Methods: Complete Guide for Beginners.
In this lesson, you learned how Python nested lists allow you to organize and manage structured data using rows and columns. A nested list is simply a list that contains one or more lists as its elements, making it an excellent choice for representing tables, matrices, spreadsheets, student records, inventory systems, and many other real-world datasets.
You began by understanding what nested lists are, why they are useful, and how to create them. You learned how to access individual values using row and column indexes and how to modify elements because nested lists are mutable.
Next, you explored how to traverse nested lists using nested for loops, add and insert new rows, append elements to existing rows, remove rows and values, and determine the number of rows and columns using the len() function.
Finally, you learned advanced concepts such as matrices, nested list comprehensions, flattening nested lists, shallow copy versus deep copy, and practical applications in student management systems, seating arrangements, and data analysis.
Nested lists are widely used in Python programming, especially in data science, machine learning, automation, scientific computing, and software development.
list[row][column].for loops are used to traverse all elements.append() and insert() methods add rows or elements.pop() and del statements remove rows or elements.len() function returns the number of rows or columns.copy.deepcopy() creates an independent copy of a nested list.A nested list is a list that contains one or more lists as its elements.
They help organize related data in rows and columns, making them suitable for tables, matrices, and structured records.
Use two indexes: list[row][column].
Yes. Nested lists are mutable, so their elements can be updated.
Use the append() method.
Use the pop() method or the del statement.
A matrix is a two-dimensional structure represented using nested lists.
A shallow copy shares inner lists, while a deep copy creates completely independent copies of all nested objects.
Use nested for loops to visit every row and every element.
Nested lists are widely used in data analysis, machine learning, game development, scientific computing, and business applications.
append().pop().for loops.for loops?Create a Python program that manages student records using nested lists.
Your program should:
copy.deepcopy().========== STUDENT MANAGEMENT SYSTEM ==========
Student Records
101 Rahul 85
102 Priya 92
103 Amit 78
104 Neha 89
Updated Records
101 Rahul 90
102 Priya 92
103 Amit 78
104 Neha 89
Total Students : 4
Backup Created Successfully
==============================================
Congratulations! You have successfully learned Python Nested Lists. You now understand how to create two-dimensional lists, access and modify nested elements, traverse rows and columns, work with matrices, use nested list comprehensions, and manage structured data efficiently.
In the next lesson, you will learn Python List Methods: Complete Guide for Beginners. You will explore all important built-in list methods such as append(), insert(), extend(), remove(), pop(), clear(), sort(), reverse(), copy(), count(), and index(), along with practical examples and real-world applications.