Matrix addition adds matching cells: C[i][j] = A[i][j] + B[i][j]. Both matrices must have the same number of rows and columns.
Remember
A + B is defined only when A and B have the same shape
[ 1 2 ] [ 5 6 ] [ 6 8 ]
[ 3 4 ] + [ 7 8 ] = [10 12 ]
Rule: (A + B)[i][j] = A[i][j] + B[i][j]
Position
A
B
A + B
[0][0]
1
5
6
[0][1]
2
6
8
[1][0]
3
7
10
[1][1]
4
8
12
Approach
How to Add Matrices in Python
Four clear steps — nested loops do the work cell by cell.
Confirm both matrices have the same rows and columns.
Create a result matrix of that same size.
Outer loop walks rows; inner loop walks columns.
Set result[i][j] = A[i][j] + B[i][j] for every cell.
Method
Idea
Time
Best for
Nested loops
Add matching cells into a new list of lists
O(m × n)
Interviews, teaching
Shape-safe add
Validate len first, then add
O(m × n)
Safer production-style code
Pseudocode
Pseudocode
function AddMatrices(A, B):
rows = rows of A
cols = cols of A
if rows != rows of B or cols != cols of B:
error "shapes must match"
C = new matrix[rows][cols]
for i from 0 to rows - 1:
for j from 0 to cols - 1:
C[i][j] = A[i][j] + B[i][j]
return C
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Rule
C[i][j] = A[i][j] + B[i][j]
Requirement
Same rows and same columns
Rows / cols
len(A) / len(A[0])
Loops
Outer = rows, inner = columns
Not multiply
Addition is entrywise only
Classic check
[[1,2],[3,4]] + [[5,6],[7,8]] = [[6,8],[10,12]]
Try it
Live Preview
Pick a sample pair to see matrices A, B, and A + B.
Tap a chip, then Add matrices (or tap a chip alone — it runs automatically).
Result
A =
1 2
3 4
B =
5 6
7 8
A + B =
6 8
10 12
Trace
Worked Walkthrough — 2×2
Visit every index once and add the matching cells.
Start with a clear nested-loop adder. Then add a shape check so mismatched sizes fail safely.
Example 1 — Nested Loops
Add two same-sized list-of-lists matrices cell by cell and print the result.
python
def add_matrices(a, b):
rows = len(a)
cols = len(a[0])
result = [[0] * cols for _ in range(rows)]
for i in range(rows):
for j in range(cols):
result[i][j] = a[i][j] + b[i][j]
return result
def print_matrix(m):
for row in m:
print(" ".join(str(x) for x in row))
a = [[1, 2], [3, 4]]
b = [[5, 6], [7, 8]]
print_matrix(add_matrices(a, b))
Output
6 8
10 12
How It Works
Read rows with len(a) and columns with len(a[0]).
Build a result matrix filled with zeros: [[0] * cols for _ in range(rows)].
Outer loop chooses row i; inner loop chooses column j.
Store a[i][j] + b[i][j] into result[i][j].
Print walks each row and joins values with spaces.
Example 2 — Shape-Safe Add
Validate dimensions before adding so mismatched matrices fail with a clear error.
python
def add_matrices(a, b):
rows = len(a)
cols = len(a[0])
if len(b) != rows or len(b[0]) != cols:
raise ValueError("Matrices must have the same dimensions.")
result = [[0] * cols for _ in range(rows)]
for i in range(rows):
for j in range(cols):
result[i][j] = a[i][j] + b[i][j]
return result
a = [[1, 2, 3], [4, 5, 6]]
b = [[7, 8, 9], [1, 1, 1]]
for row in add_matrices(a, b):
print(" ".join(str(x) for x in row))
Output
8 10 12
5 6 7
How It Works
Compare both len(...) row counts and first-row column counts.
Raise ValueError if shapes differ — addition would be undefined.
Otherwise run the same nested-loop addition as Example 1.
For the 2×3 sample: each cell sums independently.
Mention this check in interviews even if the sample sizes already match.
Edge Cases & Pitfalls
Check these before calling the solution done.
Shape
Must match
Different sizes cannot be added — validate first.
Indexing
0-based
Loops run 0 .. rows-1 and 0 .. cols-1.
Rows
Keep rows even
Every row should have the same length — avoid ragged lists.
Multiply
Different problem
Do not confuse entrywise add with row-column multiply.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops
O(m × n)
O(1) besides result
Shape-safe add
O(m × n)
O(1) besides result
Every cell is touched exactly once. The result matrix needs O(m × n) space to store the answer.