Use numpy.concatenate with sum:
print (np.concatenate(a).sum())
print (np.sum(np.concatenate(a)))
32
Performance: Depends of number of nested arrays and number of values in arrays, so best test in real data:
a = np.array([np.arange(5), np.arange(2), np.arange(7)] * 1000)
#print (a)
In [40]: %timeit np.concatenate(a).sum()
830 µs ± 22.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [41]: %timeit (np.sum(np.concatenate(a)))
835 µs ± 33.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
#original solution
In [42]: %timeit sum([np.sum(array) for array in a])
15.3 ms ± 85.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Another solutions:
In [43]: %timeit sum(np.sum(array) for array in a)
17.4 ms ± 2.27 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [44]: %timeit (sum(np.concatenate(a)))
2.28 ms ± 143 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Answer from jezrael on Stack Overflow Top answer 1 of 3
11
Use numpy.concatenate with sum:
print (np.concatenate(a).sum())
print (np.sum(np.concatenate(a)))
32
Performance: Depends of number of nested arrays and number of values in arrays, so best test in real data:
a = np.array([np.arange(5), np.arange(2), np.arange(7)] * 1000)
#print (a)
In [40]: %timeit np.concatenate(a).sum()
830 µs ± 22.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [41]: %timeit (np.sum(np.concatenate(a)))
835 µs ± 33.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
#original solution
In [42]: %timeit sum([np.sum(array) for array in a])
15.3 ms ± 85.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Another solutions:
In [43]: %timeit sum(np.sum(array) for array in a)
17.4 ms ± 2.27 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [44]: %timeit (sum(np.concatenate(a)))
2.28 ms ± 143 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2 of 3
2
While your code is good, you can also use numpy.concatenate to concatenate your arrays and then calcuate the sum via numpy.sum, python builtin sum, or a sum function over the numpy array
import numpy as np
a = np.array([np.arange(5), np.arange(2), np.arange(7)])
print(np.sum(np.concatenate(a)))
#32
print(sum(np.concatenate(a)))
#32
print(np.concatenate(a).sum())
#32
NumPy
numpy.org › doc › stable › reference › generated › numpy.sum.html
numpy.sum — NumPy v2.5 Manual
Sum of array elements over a given axis. ... Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.
03:31
Sum of NumPy Array in Python (3 Examples) | np.sum() Function of ...
03:07
np.sum vs sum - Python Numpy Tutorial - YouTube
03:18
NumPy np.sum() Tutorial: Calculate Array Sums with Axis Parameters ...
04:02
Learn NumPy aggregate functions in 4 minutes! - YouTube
04:08
sum in numpy - YouTube
Part-4 | Basic Functions of Numpy Array : sum, max, min ...
Python Examples
pythonexamples.org › python-numpy-sum-of-elements-in-array
Sum of Elements in NumPy Array - Examples
To get the sum of all elements in a NumPy array, you can use the numpy.sum() function.
Python Tutorial
pythontutorial.net › home › python numpy › numpy sum()
NumPy sum(): Calculate the Sum of Elements in an Array
August 16, 2022 - The numpy sum() function is an aggregate function that takes an array and returns the sum of all elements.
Codecademy
codecademy.com › docs › python:numpy › ndarray › .sum()
Python:NumPy | ndarray | .sum() | Codecademy
October 31, 2025 - The .sum() method returns the sum of array elements over a given axis. It can compute the sum of all elements or along specific axes in multi-dimensional arrays. ... Looking for an introduction to the theory behind programming?
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.sum.html
numpy.sum — NumPy v2.3 Manual
Sum of array elements over a given axis. ... Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.sum.html
numpy.sum — NumPy v2.1 Manual
Sum of array elements over a given axis. ... Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array.
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.sum.html
numpy.ndarray.sum — NumPy v2.5 Manual
ndarray.sum(axis=None, dtype=None, out=None, *, keepdims=<no value>, initial=<no value>, where=<no value>)#
NumPy
numpy.org › doc › 1.18 › reference › generated › numpy.sum.html
numpy.sum — NumPy v1.18 Manual
May 24, 2020 - Sum of array elements over a given axis. ... Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array.
Programiz
programiz.com › python-programming › numpy › methods › sum
NumPy sum() (With Examples)
The sum() function is used to calculate the sum of array elements along a specified axis or across all axes. The sum() function is used to calculate the sum of array elements along a specified axis or across all axes. Example import numpy as np array1 = np.array([1, 2, 3, 4, 5]) # use sum() ...
DataCamp
datacamp.com › doc › numpy › sum
NumPy sum()
Usage The `sum()` function is used to calculate the total of array elements for numerical analysis or data manipulation. It can sum values along specified axes for multi-dimensional arrays to provide insights into datasets. numpy.sum(a, axis=None, dtype=None, out=None, keepdims=<no value>, ...
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndarray.sum.html
numpy.ndarray.sum — NumPy v2.2 Manual
Return the sum of the array elements over the given axis.
NumPy
numpy.org › doc › 1.22 › › reference › generated › numpy.sum.html
numpy.sum — NumPy v1.22 Manual
Sum of array elements over a given axis. ... Elements to sum. ... Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array.