Creating a dictionary from a dictionary using a tuple Python
<p>given a dictionary with N keys and a tuple of K keys, K&lt;=N is there a pythonic way to get a dictionary with only the K keys?</p>

<p>ex. </p>

<pre><code>orig_dict = {'key1':'value1', 'key2':'value2', ..., 'keyN':'valueN'}

tuple = ('key2', 'keyM')

newdict = myFunc(orig_dict, tuple)

print newdict
</code></pre>

<p>Output:</p>

<pre><code>'key2':'value2', 'keyM':'valueM'
</code></pre>

<p>You can use a dictionary comprehension:</p>

<pre><code>{k:v for k,v in orig_dict.iteritems() if k in tuple_keys}
</code></pre>

<p>Observe:</p>

<pre><code>&gt;&gt;&gt; orig_dict = {'key1':'value1', 'key2':'value2', 'keyN':'valueN'}
&gt;&gt;&gt; tuple_keys = ('key2', 'keyN')
&gt;&gt;&gt; {k:v for k,v in orig_dict.iteritems() if k in tuple_keys}
{'keyN': 'valueN', 'key2': 'value2'}
</code></pre>

python dictionary tuples