I think this is better:

 >>> x=[[1, 2],[3, 4],[5, 6]]                                                   
>>> sum(sum(x,[]))                                                             
21
Answer from hit9 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 - num = np.arange(36): This code creates a 1D NumPy array called num with elements from 0 to 35. arr1 = np.reshape(num, [4, 9]): This code reshapes the num array into a 2D array โ€˜arr1โ€™ with 4 rows and 9 columns. result = arr1.sum(axis=0): ...
Discussions

python - How to calculate the sum of all columns of a 2D numpy array (efficiently) - Stack Overflow
However, by specifying the axis parameter you can apply an operation along the specified axis of an array: ... Sorry, I'm not sure what you mean. Summing over an axis or axes of a numpy array is done with the sum function. Is that a problem? Did you have something else in mind? More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How is Numpy sum adding up elements of a 2d array? - Stack Overflow
Now using array slicing, I can quickly obtain all items in all rows starting from the column with index 1 and sum them up: ... Just to understand the numpy array operations better, is numpy first going over all rows and summing the items of the rows, or is it going down one column, and then ... 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
Sum 2-D arrays in Python - Stack Overflow
the + would have been enough i think. but you called a sum on the result again, which collapsed the array by adding each row or whatnot. Can't know for sure because the snippet you provided can't be run as-is. ... Y*np.log(A) = ... is not valid python code ... import numpy as np arr1 = ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 30, 2018
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third party โ€บ numpy โ€บ sum()
Python Numpy sum() - Calculate Array Sum
January 1, 2025 - Define the array and then apply numpy.sum() without specifying any axis. Observe how it sums all the elements across all dimensions. PYTHON ยท Copy ยท array_2d = np.array([[1, 2, 3], [4, 5, 6]]) sum_all = np.sum(array_2d) print(sum_all) Explain ...
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)',
    )
๐ŸŒ
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))
๐ŸŒ
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 - import numpy as np #calculate sum of rows in NumPy array arr.sum(axis=1) array([ 3, 12, 21, 30, 39, 48]) The resulting array shows the sum of each row in the 2D NumPy 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.
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
Python allows us to perform the cumulative sum of array elements using the cumsum() method from the NumPy library. The numpy.cumsum() method returns the cumulative sum of elements in a given input array over a specified axis.
๐ŸŒ
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 - import numpy as np # Create a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Sum the array result = np.sum(array_2d) print("Sum of 2D array using NumPy:", result) In this case, the np.sum() function efficiently computes the sum of all elements in the 2D array.
๐ŸŒ
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

๐ŸŒ
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.
๐ŸŒ
Sharp Sight
sharpsight.ai โ€บ blog โ€บ numpy-sum
How to Use the Numpy Sum Function - Sharp Sight
February 6, 2024 - This tutorial will show you how to use the NumPy sum function. You'll learn how so sum 1-d arrays, and sum the rows and columns of 2-d NumPy arrays.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sum-2d-array-python-using-map-function
Sum 2D array in Python using map() function - GeeksforGeeks
July 21, 2022 - # Function to calculate sum of all elements in matrix # sum(arr) is a python inbuilt function which calculates # sum of each element in a iterable ( array, list etc ). # map(sum,arr) applies a given function to each item of # an iterable and ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_sum.htm
NumPy - Sum
In a two-dimensional array, you can compute the sum along a specific axis. For example, summing along the rows or columns โˆ’ ยท import numpy as np # Define a 2D array arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Sum along rows (axis=1) ...
๐ŸŒ
Drbeane
drbeane.github.io โ€บ python_dsci โ€บ pages โ€บ array_2d.html
2-Dimensional Arrays โ€” Python for Data Science
Notice that if we use np.sum() to perform row or column sums, the result is a 1D array. If we would like the result to be a 2D array, we can specify this by setting the optional keemdims parameter to True.