Python dictionary: number of keys with specific number of values
<p>I have a huge dictionary that looks like this: </p>

<pre><code>data = {'this': [{'DT': 100}], 'run': [{'NN': 215}, {'VB': 2}], 'the': [{'NNP': 6}, {'JJ': 7}, {'DT': 39517}]}
</code></pre>

<p>What I would like to to do is to run enquires that would return ,for example, the number of keys with exactly two values, in which case the answer is one because <code>'run'</code> is the only one with exactly two values <code>{'NN': 215}</code> &amp; <code>{'VB': 2}</code></p>

<p>I think this could be done with regular expression but could not find out how. </p>

<p>This will do the job:</p>

<pre><code>print len( filter( lambda x: len( x ) == 2, data.values() ) )
</code></pre>

<p>The lambda returns true when the length of an item is 2. <code>filter()</code> selects only those items where the lambda returns true, and then we count the length of the sequence returned by <code>filter()</code>. <code>data.values()</code> allows us to filter on the values from the dict rather than the keys (which is what plain <code>data</code> would've given us). Since you only wanted the count, the values are all that's needed.</p>

python dictionary