Iteration over a sub-list of dictionaries -Python
<p>I have a list of dictionaries e.g.:</p>

<pre><code>list_d = [{"a":1},{"b":2,"c":3}]
</code></pre>

<p><em>(case 1)</em></p>

<pre><code>for item in list_d:
   # add values of each sub-list's dicts
</code></pre>

<p><em>(case 2)</em></p>

<pre><code>for item in list_d[1]:
   # add values of the specific sub-list of dict
</code></pre>

<p>(case1) it returns the sum of each sub-list's dict values.</p>

<p>(case2) returns only the keys of the dictionaries.</p>

<p>Is there an efficient way to get the dictionaries of the sub-list(case2) so to add the values?</p>

<p>Here's one way to do it:</p>

<pre><code>reduce(lambda x,y: x + sum(y.values()), list_d, 0)
</code></pre>

<p>That is, starting with 0 (as the first <code>x</code>), add the sum of all values in each <code>dict</code> within <code>list_d</code>.</p>

<p>Here's another way:</p>

<pre><code>sum(sum(x.values()) for x in list_d)
</code></pre>

<p>That is, sum the (sum of values for each <code>dict</code> in <code>list_d</code>).</p>

python