Find common values in dictionary that has keys with multiple values
<p>I am new to python and I have a problem. I am trying to find keys with a value; however, the keys have multiple values.</p>

<pre><code>d = {
    'a': ['john', 'doe', 'jane'], 
    'b': ['james', 'danny', 'john'], 
    'C':['john', 'scott', 'jane'],
}
</code></pre>

<p>I want to find the value <code>john</code> in d and get the keys a, b and c or find <code>jane</code> and get keys a and c.</p>

<p>This can easily be done using a <a href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a>. It iterates over every key/value pair from the dict's items list, which contains all key/value pairs (<code>for key,val in d.items()</code>) and selects only the pairs where the target string is contained in the value list (<code>if 'john' in val</code>), building a list out of the resulting keys (<code>[ key ... ]</code>).</p>

<pre><code>&gt;&gt;&gt; [ key for key,val in d.items() if 'john' in val ]
['b', 'a', 'C']
&gt;&gt;&gt; [ key for key,val in d.items() if 'jane' in val ]
['a', 'C']
</code></pre>

python python-2.7 dictionary