I think this is better:

 >>> x=[[1, 2],[3, 4],[5, 6]]                                                   
>>> sum(sum(x,[]))                                                             
21
Answer from hit9 on Stack Overflow
๐ŸŒ
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

Discussions

python - How is Numpy sum adding up elements of a 2d array? - Stack Overflow
sum column-wise), do arr.sum(axis=0). In a 2D array, to collapse the columns (i.e. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to sum rows and columns in a 2d list?

If you want to do this without numpy:

sum_rows = [sum(x) for x in values]
sum_cols = [sum(x) for x in zip(*values)]

For larger arrays, numpy.sum is the way to go.

More on reddit.com
๐ŸŒ r/learnpython
3
4
April 18, 2016
How to produce the sum of two 2D lists by element?
You might want to flatten your lists with itertools.chain.from_iterable. Then you can simply use the sum() as in your example. If you want to keep the 2D structure, maybe numpy can help. More on reddit.com
๐ŸŒ r/learnpython
30
14
May 6, 2015
python - How to calculate the sum of all columns of a 2D numpy array (efficiently) - Stack Overflow
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ sum-2d-array-in-python-using-map-function
Sum 2D array in Python using map() function
February 12, 2020 - Using map(sum, array) is an efficient way to sum a 2D array in Python.
๐ŸŒ
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.
๐ŸŒ
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)
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third party โ€บ numpy โ€บ sum()
Python Numpy sum() - Calculate Array Sum
January 1, 2025 - This code computes the sum of array_2d as [5 7 9] when summed down the columns and [6, 15] when summed across the rows.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to produce the sum of two 2d lists by element?
r/learnpython on Reddit: How to produce the sum of two 2D lists by element?
May 6, 2015 -

As an example, if I have two lists

list1 = [[1,2], [3,4]]
list2 = [[5,6], [7,8]]

I'd like a third list to be

list3 = [[6,8], [10,12]]

I know that for a single dimensional list I would just use the syntax

newList = [sum(x) for x in zip(oldList1, oldList2)]

but I can't for the life of me figure out how to apply this to a 2D list.

What would be the most Pythonic way of doing this? I've tried fiddling with for loops but I got nowhere and I figure that it's not the best way to get it done anyway.

Thanks in advance!

EDIT: Formatting

๐ŸŒ
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.
๐ŸŒ
Bomberbot
bomberbot.com โ€บ python โ€บ mastering-2d-array-summation-in-python-with-map-a-comprehensive-guide
Mastering 2D Array Summation in Python with map(): A Comprehensive Guide - Bomberbot
The map() function is a cornerstone of functional programming in Python. It applies a given function to each item in an iterable, returning an iterator of results. The general syntax is: ... For our 2D array summation task, we'll combine map() with the built-in sum() function to create a powerful one-liner:
๐ŸŒ
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.
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 - Method 2: Using the sum() function in NumPy, numpy.sum(arr, axis, dtype, out) function returns the sum of array elements over the specified axis. To compute the sum of all columns the axis argument should be 0 in sum() function.