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>You do not need <code>regex</code> to achieve this. Regex are for parsing the strings. Better way is to create a new list to store the <code>key</code> with value as list of <code>len() == 2</code>:</p>

<pre><code>data = {'this': [{'DT': 100}], 'run': [{'NN': 215}, {'VB': 2}], 'the': [{'NNP': 6}, {'JJ': 7}, {'DT': 39517}]}
key_list = [k for k, v in data.items() if len(v) == 2]
# key_list =&gt; ['run']
</code></pre>

<p>To get the list of such values, call <code>len</code> as:</p>

<pre><code>&gt;&gt;&gt; len(key_list)
1
</code></pre>

python dictionary