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>As Antti points out it is unclear what you are asking for. I would recommend you having a look at the built-in tools in Python for <a href="https://docs.python.org/2/howto/functional.html" rel="nofollow">Functional programming</a></p>

<p>Consider the following examples:</p>

<pre><code>from operator import add

list_d = [{"a":1},{"b":2,"c":3}]

case_1 = map(lambda d: sum(d.values()), list_d)
case_2 = reduce(add, map(lambda d: sum(d.values()), list_d))
</code></pre>

python