Matrix Multiplication in Python Using List

Matrix Multiplication in Python using List | Here, we will discuss how to multiply two matrices in Python using a list. Matrix multiplication is a binary operation that multiplies two matrices, as in addition and subtraction both the matrices should be of the same size, but here in multiplication matrices need not be of the same size, but to multiply two matrices the row value of the first matrix should be equal to the column value of the second matrix. Multiplying is a bit more complex than other multiplicative operations.

Python Program to Multiply Two Matrices using List

We also multiply matrices using a list, a list is an ordered group of elements, which stores many items.

# Python program to multiply two matrices using a list

# take first matrix
A = [[1, 1, 1, 1],
     [1, 1, 1, 1],
     [1, 1, 1, 1],
     [1, 1, 1, 1]]

# take second matrix
B = [[5, 5, 5, 5],
     [4, 4, 4, 4],
     [3, 3, 3, 3],
     [2, 2, 2, 2]]

# multiply matrix
res = [[sum(a * b for a, b in zip(A_row, B_col))
        for B_col in zip(*B)] for A_row in A]
for r in res:
   print(r)

Output:-

[14, 14, 14, 14]
[14, 14, 14, 14]
[14, 14, 14, 14]
[14, 14, 14, 14]

In the code, we have used the zip function. This function helps to combine similar types of iterators, data items at that position, it also uses the shortest length of these input iterators.

Matrix Multiplication in Python Using List

In the previous program, inputs are hardcoded in the program but in this program, we take user input to multiply two matrices using a list; we intake row values and column values from the user and then proceed with the elements.

# Python program to multiply two matrices using a list

# take first matrix inputs
print("Enter order of matrix 1:")
m, n = list(map(int, input().split()))
print("Enter row values")
m1 = []
for i in range(m):
   print("Enter row ", i, " values:")
   row = list(map(int, input().split()))
   m1.append(row)

# take second matrix inputs
print("Enter order of matrix 2:")
p, q = list(map(int, input().split()))
print("Enter row values")
m2 = []
for j in range(p):
   print("Enter row ", j, "values: ")
   row = list(map(int, input().split()))
   m2.append(row)

# print both matrices
print("Matrix 1: ", m1)
print("Matrix 2: ", m2)

# multiply matrix
res = [[sum(a * b for a, b in zip(m1_row, m2_col))
        for m2_col in zip(*m2)] for m1_row in m1]
for r in res:
   print(r)

Output for the input values test-case-1:-

Enter order of matrix 1:
2 2
Enter row values
Enter row 0 values:
1 1
Enter row 1 values:
2 2
Enter order of matrix 2:
2 2
Enter row values
Enter row 0 values:
1 1
Enter row 1 values:
2 2
Matrix 1: [[1, 1], [2, 2]]
Matrix 2: [[1, 1], [2, 2]]
[3, 3]
[6, 6]

Output for the input values test-case-2:-

Enter order of matrix 1:
2 3
Enter row values
Enter row 0 values:
9 8 7
Enter row 1 values:
7 8 9
Enter order of matrix 2:
3 2
Enter row values
Enter row 0 values:
7 9
Enter row 1 values:
6 8
Enter row 2 values:
9 8
Matrix 1: [[9, 8, 7], [7, 8, 9]]
Matrix 2: [[7, 9], [6, 8], [9, 8]]
[174, 201]
[178, 199]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *