Extracting a data from dictionary in python
<pre><code>list1 = ["a","c","d"]
dict1 = {"a":9, "b":2, "c":5, "d":9, "e":6, "f":7 }
</code></pre>

<p>I want to get only list1's word from dict1.</p>

<p>The following is the result I want to get.</p>

<pre><code>{"a":9, "c":5, "d":9}
</code></pre>

<p>What am I supposed to do?</p>

<p>You can use a <em>dictionary comprehension</em> to do this:</p>

<pre><code>&gt;&gt;&gt; list1 = ["a","c","d"]
&gt;&gt;&gt; dict1 = {"a":9, "b":2, "c":5, "d":9, "e":6, "f":7 }
&gt;&gt;&gt; {k: dict1[k] for k in list1}
{'c': 5, 'd': 9, 'a': 9}
</code></pre>

<p>This works as long as the dictionary keys contains all the items in <code>list1</code>, otherwise you may use a filter, to test for items in <code>list1</code> that are not in the dictionary:</p>

<pre><code>&gt;&gt;&gt; {k: dict1[k] for k in list1 if k in dict1}
{'c': 5, 'd': 9, 'a': 9}
</code></pre>

python list dictionary