Python Dictionary counting the values against each key
<p>I have a list of dictionaries that looks like this:</p>

<pre><code>[{'id':1,'name':'Foo','age':20},{'id':2,'name':'Bar'}]
</code></pre>

<p>How can I get count of total values against each key i.e 3 against id:1</p>

<p>A more detailed problem description would have been helpful. For now I assume that you want the length of each dictionary in the list keyed against the value of the <code>id</code> key. In which case something like</p>

<pre><code>val_counts = {d['id']: len(d) for d in dlist}
</code></pre>

<p>should meet your needs. It's a dict comprehension whose keys are the <code>id</code> values and whose values are the lengths of each dictionary in the list. For your particular data the <code>val_counts</code> dictionary I get is</p>

<pre><code>{1: 3, 2: 2}
</code></pre>

python dictionary