0

Possible Duplicate:
sum each value in a list of tuples

I need help with this problem, Thank you in advance for your colaboration. I would like obtein this:

result=[12,15,18]

from

a= [[1,2,3],[4,5,6],[7,8,9]]

I am trying with the code below that works, but I would like find a general form for to do this in the case the internal lists could be variables.

lista=[[5, 7, 9], [8, 11, 13], [11, 13, 15]]

b2 = [lista [0][i]+ lista [1][i] + lista [2][i] for i in range(len(lista))]

print (b2) 

Thank you very much.

Community
  • 1
  • 1

2 Answers2

2
a= [[1,2,3],[4,5,6],[7,8,9]]
result = map(sum, zip(*a))
print result
Jun HU
  • 3,176
  • 6
  • 18
  • 21
  • complementing the helpful answer of Jun HU in python 3 could be: lista=[[1,2,3],[4,5,6],[7,8,9]] g=[] for i in map(sum, zip(*lista)): g.append(i) print (g) Thanks, – user1965825 Jan 10 '13 at 08:40
0

I would do it this way:

# a is some list
b2 = [0]*len(a[0])
for x in a:
    for y in range(len(x)):
         b2[y] += x[y]
print(b2)
Abs
  • 1,726
  • 2
  • 17
  • 28