# sum each value in a list of tuples

Posted on

### Question :

sum each value in a list of tuples

I have a list of tuples similar to this:

``````l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]
``````

I want to create a simple one-liner that will give me the following result:

``````r = (25, 20) or r = [25, 20] # don't care if tuple or list.
``````

Which would be like doing the following:

``````r = [0, 0]
for t in l:
r+=t
r+=t
``````

I am sure it is something very simple, but I can’t think of it.

Note: I looked at similar questions already:

How do I sum the first value in a set of lists within a tuple?

How do I sum the first value in each tuple in a list of tuples in Python?

Use `zip()` and `sum()`:

``````In : l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]

In : [sum(x) for x in zip(*l)]
Out: [25, 20]
``````

or:

``````In : map(sum, zip(*l))
Out: [25, 20]
``````

`timeit` results:

``````In : l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]*1000

In : %timeit [sum(x) for x in zip(*l)]
1000 loops, best of 3: 1.46 ms per loop

In : %timeit [sum(x) for x in izip(*l)]       #prefer itertools.izip
1000 loops, best of 3: 1.28 ms per loop

In : %timeit map(sum, zip(*l))
100 loops, best of 3: 1.48 ms per loop

In : %timeit map(sum, izip(*l))                #prefer itertools.izip
1000 loops, best of 3: 1.29 ms per loop
``````

If I have an array of dict e.g.

``````l = [{'quantity': 10, 'price': 5},{'quantity': 6, 'price': 15},{'quantity': 2, 'price': 3},{'quantity': 100, 'price': 2}]
``````

and i want to obtain two (or more) sums of calculated quantity over the values e.g. sum of quantities and of price*quantity

I can do:

``````(total_quantity, total_price) = (
sum(x) for x in zip(*((item['quantity'],
item['price'] * item['quantity'])
for item in l)))
``````

``````total_quantity = 0
total_price = 0
for item in l:
total_quantity += item['quantity']
total_price += item['price'] * item['quantity']
``````

Maybe the first solution is less readable, but is more “pythonesque” 🙂

``````sum(e for e in l), sum(e for e in l)