Python Program to Multiply Two Matrices
Last Updated :
27 Nov, 2025
Given two matrices, the task is to multiply them together to form a new matrix. Each element in the result is obtained by multiplying the corresponding elements of a row from the first matrix and a column from the second matrix. For Example:
Input: A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]]
Output: [[19, 22], [43, 50]]
Explanation: 1×5 + 2×7 = 19,
1×6 + 2×8 = 22,
3×5 + 4×7 = 43,
3×6 + 4×8 = 50
Let's explore different methods to multiply two matrices in Python.
Using NumPy
NumPy handles matrix multiplication internally using optimized C-based operations. It takes the rows of matrix A and the columns of matrix B, performs vectorized dot-products, and produces the result efficiently without manual loops.
Python
import numpy as np
A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[5, 8, 1, 2],
[6, 7, 3, 0],
[4, 5, 9, 1]]
r = np.dot(A, B)
for row in r:
print(row)
Output[114 160 60 27]
[74 97 73 14]
[119 157 112 23]
Explanation: np.dot(A, B) computes all row × column dot-products internally using vectorized, compiled code.
Using Transpose of B + List Comprehension
This method first converts matrix B into its transpose so that its columns become easy to iterate. Then each row of A is multiplied with each column of the transposed B, forming each cell of the result through a clean and efficient dot-product.
Python
A = [[12,7,3],
[4,5,6],
[7,8,9]]
B = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
Bt = list(zip(*B))
r = [[sum(a*b for a,b in zip(row, col)) for col in Bt] for row in A]
for row in r:
print(row)
Output[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
Explanation:
- Bt = list(zip(*B)) creates a list of columns of B so each column is readily iterable.
- [[sum(a*b for a,b in zip(row, col)) for col in Bt] for row in A] for each row in A, for each col in Bt, zip(row, col) pairs elements and sum(a*b ...) gives the dot product.
Using List Comprehension
This method computes each result cell by pairing elements of a row from A with elements of a column from B (using zip(*B)), multiplying them, and summing the products. Everything is done in a single nested list comprehension, making it concise.
Python
A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[5, 8, 1, 2],
[6, 7, 3, 0],
[4, 5, 9, 1]]
r = [[sum(a*b for a, b in zip(rA, cB)) for cB in zip(*B)] for rA in A]
for row in r:
print(row)
Output[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
Explanation:
- zip(*B) yields columns of B (recomputed for each rA).
- sum(a*b for a,b in zip(rA, cB)) pairs row and column elements and sums elementwise products to form one cell.
Using Nested Loops
This method uses the classic three-loop approach: the outer loop picks a row from A, the middle loop picks a column from B, and the inner loop multiplies corresponding elements and adds them up. It directly follows the mathematical definition of matrix multiplication.
Python
A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[5, 8, 1, 2],
[6, 7, 3, 0],
[4, 5, 9, 1]]
r = [[0]*len(B[0]) for _ in range(len(A))]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
r[i][j] += A[i][k] * B[k][j]
for row in r:
print(row)
Output[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
Explanation:
- r = [[0]*len(B[0]) for _ in range(len(A))] initializes a result matrix with zeros.
- Triple loop: outer i -> selects row of A, middle j -> selects column index of B and inner k -> multiplies A[i][k] * B[k][j] and accumulates into r[i][j].
Python Program to Multiply Two Matrices
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice