19

How do I add up all of the values of a column in a python array? Ideally I want to do this without importing any additional libraries.

input_val = [[1, 2, 3, 4, 5],
             [1, 2, 3, 4, 5],
             [1, 2, 3, 4, 5]]

output_val = [3, 6, 9, 12, 15]

I know I this can be done in a nested for loop, wondering if there was a better way (like a list comprehension)?

0

13 Answers 13

30

zip and sum can get that done:

Code:

[sum(x) for x in zip(*input_val)]

zip takes the contents of the input list and transposes them so that each element of the contained lists is produced at the same time. This allows the sum to see the first elements of each contained list, then next iteration will get the second element of each list, etc...

Test Code:

input_val = [[1, 2, 3, 4, 5],
             [1, 2, 3, 4, 5],
             [1, 2, 3, 4, 5]]

print([sum(x) for x in zip(*input_val)])

Results:

[3, 6, 9, 12, 15]
Sign up to request clarification or add additional context in comments.

Comments

12

In case you decide to use any library, numpy easily does this:

np.sum(input_val,axis=0)

2 Comments

This method is the fastest one for an array of size 15561x22417. Thanks!
You're welcome. Numpy is generally much faster than list comprehensions.
5

You may also use sum with zip within the map function:

# In Python 3.x 
>>> list(map(sum, zip(*input_val)))
[3, 6, 9, 12, 15]
# explicitly type-cast it to list as map returns generator expression

# In Python 2.x, explicit type-casting to list is not needed as `map` returns list
>>> map(sum, zip(*input_val))
[3, 6, 9, 12, 15]

Comments

3

Try this:

input_val = [[1, 2, 3, 4, 5],
         [1, 2, 3, 4, 5],
         [1, 2, 3, 4, 5]]

output_val = [sum([i[b] for i in input_val]) for b in range(len(input_val[0]))]

print output_val

Comments

3

Please construct your array using the NumPy library:

import numpy as np

create the array using the array( ) function and save it in a variable:

 arr = np.array(([1, 2, 3, 4, 5],[1, 2, 3, 4, 5],[1, 2, 3, 4, 5]))

apply sum( ) function to the array specifying it for the columns by setting the axis parameter to zero:

arr.sum(axis = 0)

Comments

2

This should work:

[sum(i) for i in zip(*input_val)]

Comments

2

I guess you can use:

import numpy as np
new_list = sum(map(np.array, input_val))

Comments

2

I think this is the most pythonic way of doing this

map(sum, [x for x in zip(*input_val)])

Comments

1

One-liner using list comprehensions: for each column (length of one row), make a list of all the entries in that column, and sum that list.

output_val = [sum([input_val[i][j] for i in range(len(input_val))]) \
                 for j in range(len(input_val[0]))]

Comments

1

Try this code. This will make output_val end up as [3, 6, 9, 12, 15] given your input_val:

input_val = [[1, 2, 3, 4, 5],
             [1, 2, 3, 4, 5],
             [1, 2, 3, 4, 5]]

vals_length = len(input_val[0])
output_val = [0] * vals_length # init empty output array with 0's
for i in range(vals_length): # iterate for each index in the inputs
    for vals in input_val:
        output_val[i] += vals[i] # add to the same index

print(output_val) # [3, 6, 9, 12, 15]

Comments

1

Using Numpy you can easily solve this issue in one line:

1: Input

input_val = [[1, 2, 3, 4, 5],
             [1, 2, 3, 4, 5],
             [1, 2, 3, 4, 5]]

2: Numpy does the math for you

np.sum(input_val,axis=0)

3: Then finally the results

array([ 3,  6,  9, 12, 15])

Comments

0
output_val=input_val.sum(axis=0)

this would make the code even simpler I guess

1 Comment

Running this with the above inputs throws AttributeError: 'list' object has no attribute 'sum'
0

You can use the sum function instead of np.sum simply.

input_val = np.array([[1, 2, 3, 4, 5],
         [1, 2, 3, 4, 5],
         [1, 2, 3, 4, 5]])
sum(input_val)

output: array([ 3,  6,  9, 12, 15])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.