Summation Matrix columns - Python
Last Updated :
12 Jul, 2025
The task of summing the columns of a matrix in Python involves calculating the sum of each column in a 2D list or array. For example, given the matrix a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]], the goal is to compute the sum of each column, resulting in [13, 13, 13].
Using numpy.sum()
numpy.sum() is a highly optimized function from the numpy library that efficiently handles mathematical operations on matrices . It can directly sum the columns of a matrix by specifying the axis as 0.
Python
import numpy as np
a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
a = np.array(a) # Converting `a` to numpy array
res = np.sum(a, axis=0).tolist()
print(res)
Explanation: np.sum(a, axis=0) calculates sum the elements of each column. Then result is converted back to a list with tolist() .
Using list comprehension
List comprehension is a concise way of generating lists in Python. When summing columns, we use the zip(*a) technique to transpose the matrix and iterate over each column to apply sum().
Python
a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
res = [sum(idx) for idx in zip(*a)]
print(res)
Explanation: This code transposes the matrix using zip(*a), sums each column with sum(idx) and stores the results in a list.
itertools.starmap() applies a function to the items in an iterable. When used with sum() and zip(*a), it sums the columns in a similar way to the list comprehension approach.
Python
import itertools
a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
res = list(itertools.starmap(lambda *args: sum(args), zip(*a)))
print(res)
Explanation: itertools.starmap() apply the sum() function to each transposed column from zip(*a). It sums the elements of each column and stores the results in a list.
Using for loop
A traditional for loop iterates over the transposed matrix using zip(*a) and sums the elements in each column manually.
Python
a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]]
res = []
for col in zip(*a):
res.append(sum(col))
print(res)
Explanation: This code transposes the matrix with zip(*a), sums each column in a for loop and appends the results to a list.
Similar Reads
Python - Tuple Matrix Columns Summation Sometimes, while working with Tuple Matrix, we can have a problem in which we need to perform summation of each column of tuple matrix, at the element level. This kind of problem can have application in Data Science domains. Let's discuss certain ways in which this task can be performed. Input : tes
8 min read
Python - Summation of kth column in a matrix Sometimes, while working with Python Matrix, we may have a problem in which we require to find the summation of a particular column. This can have a possible application in day-day programming and competitive programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Us
8 min read
Python | Matrix True Summation Checking a number/element by a condition is a common problem one faces and is done in almost every program. Sometimes we also require to get the totals that match the particular condition to have a distinguish which to not match for further utilization like in data Science. Let's discuss certain way
8 min read
Python | Column summation of tuples Sometimes, we encounter a problem where we deal with a complex type of matrix column summation in which we are given a tuple and we need to perform the summation of its like elements. This has a good application in Machine Learning domain. Let's discuss certain ways in which this can be done. Method
7 min read
Python | Cumulative Columns summation of Records Sometimes, while working with records, we can have a problem in which we need to sum all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using sum() + list
7 min read
Element indices Summation - Python Our task is to calculate the sum of elements at specific indices in a list. This means selecting elements at specific positions and computing their sum. Given a list and a set of indices, the goal is to compute the sum of elements present at those indices. For example, given the list [10, 20, 30, 40
3 min read