Two sums from one list

list, python

Solution

You can try this:

from itertools import izip
sample = [(1,3), (4,5), (8,2)]
t1, t2 = map(sum, izip(*sample))

You can also use a list comprehension instead of `map`.

from itertools import izip
sample = [(1,3), (4,5), (8,2)]
t1, t2 = [sum(t) for t in izip(*sample)]

And you can deal with more than two sums:

from itertools import izip
sample = [(1, 3, 1), (4, 5, 1), (8, 2, 1)]
sums = [sum(t) for t in izip(*sample)]
# sums == [13, 10, 3]

Problem

I'd like to get the sums for two different values in a list. For example: ``` sample = [(1,3), (4,5), (8,2)] ``` I'd like the output to be ``` 13, 10 ``` I could do it in a couple of different ways. Here's how I have it currently: ``` t1 = 0 t2 = 0 for item1, item2 in sample: t1 += item1 t2 += item2 ``` What would be a more Pythonic way to solve this?

Original source