Use zip

col_totals = [ sum(x) for x in zip(*my_list) ]
Answer from Metalshark on Stack Overflow
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-152.php
Python NumPy: Calculate the sum of all columns of a 2D NumPy array - w3resource
August 29, 2025 - # Importing the NumPy library and aliasing it as 'np' import numpy as np # Generating a NumPy array 'num' containing numbers from 0 to 35 using arange() num = np.arange(36) # Reshaping the array 'num' into a 4x9 matrix and assigning it to 'arr1' arr1 = np.reshape(num, [4, 9]) # Displaying a message indicating the original array will be printed print("Original array:") # Printing the original array 'arr1' print(arr1) # Computing the sum of each column of the array 'arr1' along axis 0 (columns) and storing it in 'result' result = arr1.sum(axis=0) # Displaying a message indicating the sum of all columns will be printed print("\nSum of all columns:") # Printing the sum of each column of the array 'arr1' print(result)
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to sum rows and columns in a 2d list?
r/learnpython on Reddit: How to sum rows and columns in a 2d list?
April 18, 2016 -

I am trying to figure out how to sum each individual row, and then each column. Here is what I have so far:

import random

rows = 3

cols = 3

def main():

values = [[0,0,0], [0,0,0], [0,0,0]]
for r in range(rows):
    for c in range(cols):
        values[r][c] = random.randint(1, 4)
print('List')
print(values)

main()

Sum a row from a 2D Array Mar 30, 2022
r/learnprogramming
4y ago
sum of row and column in a table Jul 4, 2021
r/learnpython
5y ago
How to sum an array together in python? Apr 9, 2019
r/learnpython
7y ago
Explain 2D Lists Aug 3, 2014
r/learnpython
12y ago
How to do a quick summation using NumPy? Dec 14, 2022
r/learnpython
3y ago
More results from reddit.com
Discussions

python - How do I sum the columns in 2D list? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to sum a 2d array in Python? - Stack Overflow
1 Python: Iterate over a sub-2d (nested) array to calculate its sum More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to calculate the sum of all columns of a 2D numpy array (efficiently) - Stack Overflow
should solve the problem. It is a 2d np.array and you will get the sum of all column. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Can you explain why/how this works? 2D array and sum()
Does this help? >>> []+[3,2,3]+[23,4,4]+[5,43,3] [3, 2, 3, 23, 4, 4, 5, 43, 3] More on reddit.com
๐ŸŒ r/learnpython
6
1
January 27, 2023
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ calculate-the-sum-of-all-columns-in-a-2d-numpy-array
Calculate the sum of all columns in a 2D NumPy array - GeeksforGeeks
July 21, 2021 - # importing required libraries import numpy # explicit function to compute column wise sum def colsum(arr, n, m): for i in range(n): su = 0; for j in range(m): su += arr[j][i] print(su, end = " ") # creating the 2D Array TwoDList = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] TwoDArray = numpy.array(TwoDList) # displaying the 2D Array print("2D Array:") print(TwoDArray) # printing the sum of each column print("\nColumn-wise Sum:") colsum(TwoDArray, len(TwoDArray[0]), len(TwoDArray))
Top answer
1 of 6
162

Check out the documentation for numpy.sum, paying particular attention to the axis parameter. To sum over columns:

>>> import numpy as np
>>> a = np.arange(12).reshape(4,3)
>>> a.sum(axis=0)
array([18, 22, 26])

Or, to sum over rows:

>>> a.sum(axis=1)
array([ 3, 12, 21, 30])

Other aggregate functions, like numpy.mean, numpy.cumsum and numpy.std, e.g., also take the axis parameter.

From the Tentative Numpy Tutorial:

Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the ndarray class. By default, these operations apply to the array as though it were a list of numbers, regardless of its shape. However, by specifying the axis parameter you can apply an operation along the specified axis of an array:

2 of 6
13

Other alternatives for summing the columns are

numpy.einsum('ij->j', a)

and

numpy.dot(a.T, numpy.ones(a.shape[0]))

If the number of rows and columns is in the same order of magnitude, all of the possibilities are roughly equally fast:

If there are only a few columns, however, both the einsum and the dot solution significantly outperform numpy's sum (note the log-scale):


Code to reproduce the plots:

import numpy
import perfplot


def numpy_sum(a):
    return numpy.sum(a, axis=1)


def einsum(a):
    return numpy.einsum('ij->i', a)


def dot_ones(a):
    return numpy.dot(a, numpy.ones(a.shape[1]))


perfplot.save(
    "out1.png",
    # setup=lambda n: numpy.random.rand(n, n),
    setup=lambda n: numpy.random.rand(n, 3),
    n_range=[2**k for k in range(15)],
    kernels=[numpy_sum, einsum, dot_ones],
    logx=True,
    logy=True,
    xlabel='len(a)',
    )
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to sum the rows and columns of a numpy array
How to Sum the Rows and Columns of a NumPy Array
January 24, 2023 - The resulting array shows the sum of each row in the 2D NumPy array. ... The sum of values in the first row is 0 + 1 + 2 = 3. The sum of values in the first row is 3 + 4 + 5 = 12. The sum of values in the first row is 6 + 7 + 8 = 21. ... The resulting array shows the sum of each column in the 2D NumPy array.
Find elsewhere
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-find-the-cumulative-sum-of-a-2d-array-in-python
How to find the cumulative sum of a 2D array in Python
The numpy.cumsum() method returns the cumulative sum of the elements in a given input array over a specified axis.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ can you explain why/how this works? 2d array and sum()
r/learnpython on Reddit: Can you explain why/how this works? 2D array and sum()
January 27, 2023 -
state=[[3,2,3],[23,4,4],[5,43,3]]
s=sum(state, [])

This code transforms a 2D array to 1D array (all elements of the 2D array are now elements of an 1D array). I know that sum() first parameter sums all the elements of the iterable and the second is the starting point. But i really dont get how this leads to 2D--->1D

๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ how-to-calculate-the-sum-of-all-columns-of-a-2d-numpy-array-efficiently.aspx
Python - How to calculate the sum of all columns of a 2D numpy array (efficiently)?
October 10, 2023 - For this purpose, we will use the NumPy sum method while paying some extra attention to the axis argument, to sum over the columns we will use the argument axis=0. ... # Import numpy import numpy as np # Creating a numpy array arr = np.arange(12).reshape(4,3) # Display original array ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ calculating-the-sum-of-all-columns-of-a-2d-numpy-array
Calculating the sum of all columns of a 2D NumPy array - GeeksforGeeks
July 15, 2025 - # importing numpy import numpy as np # initialize the 2-d array arr = np.array([[1, 2, 3, 4, 5], [5, 6, 7, 8, 9], [2, 1, 5, 7, 8], [2, 9, 3, 1, 0]]) # calculating column wise sum sum_2d = np.einsum('ij->j', arr) # displaying the sum print("Column wise sum is :\n", sum_2d) Output : Column wise sum is : [10 18 18 20 22] Comment ยท Python Fundamentals ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ sum-2d-array-python-using-map-function
Sum 2D array in Python using map() function - GeeksforGeeks
July 21, 2022 - def findSum(arr): # inner map function applies inbuilt function # sum on each row of matrix arr and returns # list of sum of elements of each row return sum(map(sum,arr)) # Driver function if __name__ == "__main__": arr = [[1, 2, 3], [4, 5, 6], [2, 1, 2]] print ("Sum = ",findSum(arr)) ... What does map() do? The map() function applies a given function to each item of an iterable(list, tuple etc.) and returns a list of the results. For example see given below example : ... # Python code to demonstrate working of map() # Function to calculate square of any number def calculateSquare(n): return n*n # numbers is a list of elements numbers = [1, 2, 3, 4] # Here map function is mapping calculateSquare # function to each element of numbers list.
๐ŸŒ
pythontutorials
pythontutorials.net โ€บ blog โ€บ how-to-calculate-the-sum-of-all-columns-of-a-2d-numpy-array-efficiently
How to Efficiently Calculate the Sum of Columns in a 2D NumPy Array (Without Loops) โ€” pythontutorials.net
The most efficient way to compute column sums in NumPy is with numpy.sum(), using the axis parameter to specify the dimension to aggregate. NumPy arrays have axes (dimensions). For a 2D array:
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.sum.html
numpy.sum โ€” NumPy v2.5 Manual
Elements to include in the sum. See reduce for details. ... An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned.
๐ŸŒ
USAVPS
usavps.com โ€บ home โ€บ blog โ€บ python tutorial: how to sum 2d arrays in python?
Python Tutorial: How to Sum 2D Arrays in Python? - USAVPS
March 18, 2026 - In this case, the np.sum() function efficiently computes the sum of all elements in the 2D array. The output will be the same: ... Another Pythonic way to sum a 2D array is by using list comprehensions.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy-sum-in-python
numpy.sum() in Python - GeeksforGeeks
August 28, 2024 - This Python program uses numpy.sum() to compute the sum of elements in a 2D array. It calculates the total sum, sums along rows (axis=0), sums along columns (axis=1), and sums along columns while keeping the dimensions (keepdims=True).
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 44597620 โ€บ sum-columns-of-parts-of-2d-array-python
Sum columns of parts of 2D array Python - Stack Overflow
June 16, 2017 - from itertools import groupby M ... ] print([ [ key[0], key[1], sum(x[2] for x in group), sum(x[3] for x in group), ] for key, group in [ (key, list(group)) for key, group in groupby(sorted(M), lambda x: (x[0], x[1])) ] ])...
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ basic โ€บ numpy-basic-exercise-32.php
NumPy: Compute sum of all elements, sum of each column and sum of each row of a given array - w3resource
August 28, 2025 - Original array: [[0 1] [2 3]] Sum of all elements: 6 Sum of each column: [2 4] Sum of each row: [1 5] ... โ€˜x = np.array([[0, 1], [2, 3]])โ€™ creates a 2D array 'x' with the shape (2, 2) and elements [[0, 1], [2, 3]].