How to sum list of tuples?
<p>I have a list of tuples:</p>

<pre><code>[ (a,1), (a,2), (b,1), (b,3) ]
</code></pre>

<p>I want to get the sum of both the <code>a</code> and <code>b</code> values. The results should be in this format:</p>

<pre><code>[ { 'key' : a, 'value' : 3 }, {'key' : b, 'value' : 4 } ]
</code></pre>

<p>How can I do this?</p>

<pre><code>from itertools import groupby
[{'key': k, 'value': sum(v for _,v in g)} for k, g in groupby(sorted(lst), key = lambda x: x[0])]

# [{'key': 'a', 'value': 3}, {'key': 'b', 'value': 4}]
</code></pre>

python