Table of contents
1.
Introduction
2.
Add Two Matrices Using For Loop
3.
Add Two Matrices Using List Comprehension
4.
Add Two Matrices Using zip() Function
5.
Add Two Matrices Using NumPy
6.
Add Two Matrices Using SymPy
7.
Matrix Addition using Nested Loops
8.
Frequently Asked Questions
8.1.
What is matrix addition in Python?
8.2.
Which method is best for adding matrices in Python?
8.3.
Can I add matrices of different sizes in Python?
9.
Conclusion
Last Updated: Aug 11, 2025
Medium

Matrix Addition in Python

Author Rahul Singh
0 upvote

Introduction

Matrix addition is a fundamental operation in mathematics and computer science, especially in fields like data science, machine learning, and engineering. It involves adding corresponding elements of two matrices to form a new matrix. In Python, matrix addition can be performed in several ways using loops, list comprehension, built-in functions like zip(), and libraries such as NumPy and SymPy. 

In this article, we will discuss various methods to add two matrices in Python, with clear examples and explanations.

Add Two Matrices Using For Loop

The easiest way to add two matrices in Python is by using a for loop. This method involves iterating through each element of the matrices, adding corresponding elements, and storing the result in a new matrix.

Example:

# Define two matrices
matrix1 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]


matrix2 = [
    [9, 8, 7],
    [6, 5, 4],
    [3, 2, 1]
]


# Initialize an empty matrix to store the result
result = []


# Use nested loops to add the matrices
for i in range(len(matrix1)):
    row = []
    for j in range(len(matrix1[0])):
        row.append(matrix1[i][j] + matrix2[i][j])
    result.append(row)


# Print the result
for row in result:
    print(row)
You can also try this code with Online Python Compiler
Run Code


Output:

[10, 10, 10]
[10, 10, 10]
[10, 10, 10]


In this example, we first define two matrices matrix1 and matrix2. We then use a nested for loop to iterate through each element in the matrices, add the corresponding elements, and store the result in a new matrix called result.

Add Two Matrices Using List Comprehension

List comprehension is a more Pythonic way of adding two matrices. It allows you to create a new list in a single line of code, making it more concise and readable.

Example:

# Define two matrices
matrix1 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

matrix2 = [
    [9, 8, 7],
    [6, 5, 4],
    [3, 2, 1]
]

# Add matrices using list comprehension
result = [[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in range(len(matrix1))]

# Print the result
for row in result:
    print(row)
You can also try this code with Online Python Compiler
Run Code


Output:

[10, 10, 10]
[10, 10, 10]
[10, 10, 10]


In this approach, we use list comprehension to iterate through each row and column of the matrices and add corresponding elements. This method is more efficient and shorter than using for loops.

Add Two Matrices Using zip() Function

The zip() function in Python is a powerful tool that allows you to iterate over two or more lists simultaneously. We can use zip() to add two matrices by combining corresponding rows from both matrices and then summing their elements.

Example:

# Define two matrices
matrix1 = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]


matrix2 = [
    [9, 8, 7],
    [6, 5, 4],
    [3, 2, 1]
]


# Add matrices using zip() function
result = [[x + y for x, y in zip(matrix1_row, matrix2_row)] for matrix1_row, matrix2_row in zip(matrix1, matrix2)]


# Print the result
for row in result:
    print(row)
You can also try this code with Online Python Compiler
Run Code


Output:

[10, 10, 10]
[10, 10, 10]
[10, 10, 10]


Here, zip() combines the corresponding rows of matrix1 and matrix2. The inner list comprehension adds the elements from each row together, and the result is stored in the result matrix.

Add Two Matrices Using NumPy

NumPy is a powerful library for numerical computations in Python. It provides an easy way to work with matrices and perform matrix operations. Adding two matrices in NumPy is as easy as using the + operator.

Example:

import numpy as np


# Define two matrices
matrix1 = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])


matrix2 = np.array([
    [9, 8, 7],
    [6, 5, 4],
    [3, 2, 1]
])


# Add matrices using NumPy
result = matrix1 + matrix2


# Print the result
print(result)
You can also try this code with Online Python Compiler
Run Code

 

Output:

[[10 10 10]
 [10 10 10]
 [10 10 10]]


In this example, we use the + operator to add the two NumPy arrays matrix1 and matrix2. NumPy automatically performs element-wise addition and returns the result in a new array.

Add Two Matrices Using SymPy

SymPy is a symbolic mathematics library for Python that can be used for matrix operations, including addition. SymPy allows you to work with symbolic expressions and perform matrix operations with more flexibility.

Example:

import sympy as sp


# Define two matrices using SymPy
matrix1 = sp.Matrix([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
])


matrix2 = sp.Matrix([
    [9, 8, 7],
    [6, 5, 4],
    [3, 2, 1]
])


# Add matrices using SymPy
result = matrix1 + matrix2


# Print the result
print(result)
You can also try this code with Online Python Compiler
Run Code


Output:

Matrix([[10, 10, 10], [10, 10, 10], [10, 10, 10]])


In this case, we use SymPy’s Matrix class to define matrices and add them using the + operator. SymPy handles symbolic computation and provides a matrix object for easier manipulation.

Matrix Addition using Nested Loops

To add two matrices using nested loops in Python, we first need to make sure the matrices have the same dimensions. This means they should have the same number of rows & columns. If the matrices are not the same size, the addition cannot be performed.

Let’s see how we can add two matrices using nested loops in Python:

# Matrix A
A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]

# Matrix B 
B = [[9, 8, 7],
     [6, 5, 4],
     [3, 2, 1]]

# Result matrix to store the sum
result = [[0, 0, 0],
          [0, 0, 0],
          [0, 0, 0]]

# Iterate over rows
for i in range(len(A)):
    # Iterate over columns
    for j in range(len(A[0])):
        result[i][j] = A[i][j] + B[i][j]

# Print the result matrix
for r in result:
    print(r)
You can also try this code with Online Python Compiler
Run Code


In this code, we define two matrices A & B, each with 3 rows & 3 columns. We also create a result matrix with the same dimensions to store the sum.
 

We then use two nested loops to iterate over the rows & columns of the matrices. The outer loop iterates over the rows using the variable i, while the inner loop iterates over the columns using the variable j.
 

Inside the nested loops, we add the corresponding elements of matrices A & B and store the result in the result matrix at the same position.
 

Finally, we print the result matrix to display the sum of the two matrices.

The output will be:

[10, 10, 10]
[10, 10, 10] 
[10, 10, 10]


This shows the element-wise addition of the two matrices A & B.

Frequently Asked Questions

What is matrix addition in Python?

Matrix addition involves adding corresponding elements of two matrices to produce a new matrix. In Python, you can perform matrix addition using various methods like loops, list comprehension, the zip() function, or libraries like NumPy and SymPy.

Which method is best for adding matrices in Python?

For beginners, using for loops or list comprehension is easy to understand. However, if you're working with large matrices, libraries like NumPy provide optimized solutions for matrix operations and are much faster.

Can I add matrices of different sizes in Python?

No, matrix addition requires that both matrices have the same dimensions. If the dimensions do not match, you will get an error. You can check the dimensions using len(matrix) and ensure they are equal before performing the addition.

Conclusion

In this article, we’ve discussed different ways to add two matrices in Python. Whether you're using for loops, list comprehension or libraries like NumPy, each method has its own advantages. We’ve learned how to implement these techniques with examples and understand the outputs. Mastering matrix addition in Python will help you work with more complex mathematical problems and algorithms in the future.

You can also check out our other blogs on Code360.

Live masterclass