How to sum a 2d array in Python?

Question:

I want to sum a 2 dimensional array in python:

Here is what I have:

def sum1(input):
    sum = 0
    for row in range (len(input)-1):
        for col in range(len(input[0])-1):
            sum = sum + input[row][col]

    return sum


print sum1([[1, 2],[3, 4],[5, 6]])

It displays 4 instead of 21 (1+2+3+4+5+6 = 21). Where is my mistake?

Answers:

This is the issue

for row in range (len(input)-1):
    for col in range(len(input[0])-1):

try

for row in range (len(input)):
    for col in range(len(input[0])):

Python’s range(x) goes from 0..x-1 already

range(…)
range([start,] stop[, step]) -> list of integers

Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
Answered By: dfb

range() in python excludes the last element. In other words, range(1, 5) is [1, 5) or [1, 4]. So you should just use len(input) to iterate over the rows/columns.

def sum1(input):
    sum = 0
    for row in range (len(input)):
        for col in range(len(input[0])):
            sum = sum + input[row][col]

    return sum
Answered By: spinlok

Don’t put -1 in range(len(input)-1) instead use:

range(len(input))

range automatically returns a list one less than the argument value so no need of explicitly giving -1

Answered By: Kartik Anand

And numpy solution is just:

import numpy as np
x = np.array([[1, 2],[3, 4],[5, 6]])

Result:

>>> b=np.sum(x)
   print(b)
21
Answered By: Akavall

You could rewrite that function as,

def sum1(input):
    return sum(map(sum, input))

Basically, map(sum, input) will return a list with the sums across all your rows, then, the outer most sum will add up that list.

Example:

>>> a=[[1,2],[3,4]]
>>> sum(map(sum, a))
10
Answered By: machow

Better still, forget the index counters and just iterate over the items themselves:

def sum1(input):
    my_sum = 0
    for row in input:
        my_sum += sum(row)
    return my_sum

print sum1([[1, 2],[3, 4],[5, 6]])

One of the nice (and idiomatic) features of Python is letting it do the counting for you. sum() is a built-in and you should not use names of built-ins for your own identifiers.

Answered By: msw

I think this is better:

 >>> x=[[1, 2],[3, 4],[5, 6]]                                                   
>>> sum(sum(x,[]))                                                             
21
Answered By: hit9

This is yet another alternate Solution

In [1]: a=[[1, 2],[3, 4],[5, 6]]
In [2]: sum([sum(i) for i in a])
Out[2]: 21
Answered By: Ajay

Quick answer, use…

total = sum(map(sum,[array]))

where [array] is your array title.

Answered By: Finger Picking Good
def sum1(input):
    return sum([sum(x) for x in input])
Answered By: J F Fitch

In Python 3.7

import numpy as np
x = np.array([ [1,2], [3,4] ])
sum(sum(x))

outputs

10
Answered By: Richard H Downey

It seems like a general consensus is that numpy is a complicated solution. In comparison to simpler algorithms. But for the sake of the answer being present:

import numpy as np


def addarrays(arr):

    b = np.sum(arr)
    return sum(b)


array_1 = [
  [1, 2],
  [3, 4],
  [5, 6]
]
print(addarrays(array_1))

This appears to be the preferred solution:

x=[[1, 2],[3, 4],[5, 6]]                                                   
sum(sum(x,[]))                                                             
Answered By: peyo
def sum1(input):
    sum = 0
    for row in input:
        for col in row:
            sum += col
    return sum
print(sum1([[1, 2],[3, 4],[5, 6]]))
Answered By: Mbuthi Mungai

Speed comparison

import random
import timeit
import numpy
x = [[random.random() for i in range(100)] for j in range(100)]
xnp = np.array(x)

Methods

print("Sum python array:")
%timeit sum(map(sum,x))
%timeit sum([sum(i) for i in x])
%timeit sum(sum(x,[]))
%timeit sum([x[i][j] for i in range(100) for j in range(100)])

print("Convert to numpy, then sum:")
%timeit np.sum(np.array(x))
%timeit sum(sum(np.array(x)))

print("Sum numpy array:")
%timeit np.sum(xnp)
%timeit sum(sum(xnp))

Results

Sum python array:
130 µs ± 3.24 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
149 µs ± 4.16 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
3.05 ms ± 44.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
2.58 ms ± 107 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Convert to numpy, then sum:
1.36 ms ± 90.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
1.63 ms ± 26.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Sum numpy array:
24.6 µs ± 1.95 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
301 µs ± 4.78 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Answered By: Fasmo
def sum1(input):
    sum = 0
    for row in range (len(input)-1):
        for col in range(len(input[0])-1):
            sum = sum + input[row][col]

    return sum


print (sum1([[1, 2],[3, 4],[5, 6]]))

You had a problem with parenthesis at the print command….
This solution will be good now
The correct solution in Visual Studio Code

Answered By: OTIENO BRIAN
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.