Nested List in Python
A nested list is a list inside another list.
It allows storing tabular or hierarchical data.
👉 Example:
nested = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
Here:
First sublist → [1, 2, 3]
Second sublist → [4, 5]
Third sublist → [6, 7, 8, 9]
🔹 1. Accessing Elements in Nested Lists
matrix = [[10, 20, 30], [40, 50, 60]]
print(matrix[0]) # First sublist
print(matrix[1][2]) # Second sublist, third element
Output:
[10, 20, 30]
60
🔹 2. Iterating Through a Nested List
matrix = [[1, 2], [3, 4], [5, 6]]
for row in matrix:
for item in row:
print(item, end=" ")
Output:
123456
🔹 3. Creating a Matrix (2D List)
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print("Matrix:")
for row in matrix:
print(row)
Output:
Matrix:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
🔹 4. List Comprehension with Nested List
matrix = [[x*y for y in range(1, 4)] for x in range(1, 4)]
print(matrix)
Output:
[[1, 2, 3], [2, 4, 6], [3, 6, 9]]
👉 This creates a multiplication table (3 × 3).
🔹 5. Nested List as Student Records
students = [
["Amit", 85, 90],
["Riya", 78, 88],
["Rahul", 92, 81]
]
for student in students:
name = student[0]
avg = (student[1] + student[2]) / 2
print(name, ":", avg)
Output:
Amit : 87.5
Riya : 83.0
Rahul : 86.5
🔹 6. Updating Nested List Elements
matrix = [[1, 2], [3, 4]]
matrix[0][1] = 99
print(matrix)
Output:
[[1, 99], [3, 4]]
🔹 7. Flattening a Nested List
nested = [[1, 2], [3, 4], [5, 6]]
flat = []
for sublist in nested:
for item in sublist:
[Link](item)
print(flat)
Output:
[1, 2, 3, 4, 5, 6]
Important Matrix Programs in Python (Class 11 CBSE)
🔹 1. Input and Display a Matrix
rows = 2
cols = 3
matrix = []
print("Enter elements:")
for i in range(rows):
row = []
for j in range(cols):
val = int(input(f"Enter element[{i}][{j}]: "))
[Link](val)
[Link](row)
print("\nMatrix:")
for r in matrix:
print(r)
🔹 2. Sum of All Elements
matrix = [[1, 2, 3], [4, 5, 6]]
total = 0
for row in matrix:
total += sum(row)
print("Sum of all elements:", total)
Output:
Sum of all elements: 21
🔹 3. Row-wise Sum
matrix = [[1, 2, 3], [4, 5, 6]]
for i in range(len(matrix)):
print("Sum of Row", i, "=", sum(matrix[i]))
Output:
Sum of Row 0 = 6
Sum of Row 1 = 15
🔹 4. Column-wise Sum
matrix = [[1, 2, 3], [4, 5, 6]]
rows, cols = len(matrix), len(matrix[0])
for j in range(cols):
col_sum = 0
for i in range(rows):
col_sum += matrix[i][j]
print("Sum of Column", j, "=", col_sum)
Output:
Sum of Column 0 = 5
Sum of Column 1 = 7
Sum of Column 2 = 9
🔹 5. Transpose of a Matrix
matrix = [[1, 2, 3], [4, 5, 6]]
rows, cols = len(matrix), len(matrix[0])
transpose = [[0]*rows for _ in range(cols)]
for i in range(rows):
for j in range(cols):
transpose[j][i] = matrix[i][j]
print("Transpose:")
for r in transpose:
print(r)
Output:
Transpose:
[1, 4]
[2, 5]
[3, 6]
🔹 6. Addition of Two Matrices
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
result = [[0, 0], [0, 0]]
for i in range(len(A)):
for j in range(len(A[0])):
result[i][j] = A[i][j] + B[i][j]
print("Matrix Addition:")
for r in result:
print(r)
Output:
Matrix Addition:
[6, 8]
[10, 12]
🔹 7. Multiplication of Two Matrices
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
result = [[0, 0], [0, 0]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
print("Matrix Multiplication:")
for r in result:
print(r)
Output:
Matrix Multiplication:
[19, 22]
[43, 50]
🔹 8. Diagonal Elements (Square Matrix)
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print("Principal Diagonal:", [matrix[i][i] for i in range(3)])
print("Secondary Diagonal:", [matrix[i][2-i] for i in range(3)])
Output:
Principal Diagonal: [1, 5, 9]
Secondary Diagonal: [3, 5, 7]
🔹 9. Upper and Lower Triangular Matrix
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print("Upper Triangular:")
for i in range(3):
for j in range(3):
if i > j:
print(0, end=" ")
else:
print(matrix[i][j], end=" ")
print()
print("\nLower Triangular:")
for i in range(3):
for j in range(3):
if i < j:
print(0, end=" ")
else:
print(matrix[i][j], end=" ")
print()
Output:
Upper Triangular:
123
056
009
Lower Triangular:
100
450
789
🔹 10. Identity Matrix
n=4
identity = [[0]*n for _ in range(n)]
for i in range(n):
identity[i][i] = 1
print("Identity Matrix:")
for r in identity:
print(r)
Output:
Identity Matrix:
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]