Way to get specific keys from dictionary
<p>I am looking for a way to get specific keys from a dictionary. </p>

<p>In the example below, I am trying to <code>get all keys except 'inside'</code></p>

<pre><code>&gt;&gt;&gt; d
{'shape': 'unchanged', 'fill': 'unchanged', 'angle': 'unchanged', 'inside': 'a-&gt;e', 'size': 'unchanged'}
&gt;&gt;&gt; d_keys = list(set(d.keys()) - set(["inside"]) )
&gt;&gt;&gt; d_keys
['shape', 'fill', 'angle', 'size']
&gt;&gt;&gt; for key in d_keys:
...     print "key: %s, value: %s" % (key, d[key])
...
key: shape, value: unchanged
key: fill, value: unchanged
key: angle, value: unchanged
key: size, value: unchanged
</code></pre>

<p>Is there a better way to do this than above?</p>

<p>a cross-version approach:</p>

<pre><code>d = {'shape': 'unchanged', 'fill': 'unchanged',
    'angle': 'unchanged', 'inside': 'a-&gt;e', 'size': 'unchanged'}

d_keys = [k for k in d.keys() if k  != 'inside']

print(d_keys)
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>['fill', 'shape', 'angle', 'size']
</code></pre>

<p><hr>
expanding it a bit:</p>

<pre><code>def get_pruned_dict(d, excluded_keys):
    return {k:v for k,v in d.items() if k not in excluded_keys}

exclude = ('shape', 'inside')
pruned_d = get_pruned_dict(d, exclude)

print(pruned_d)
</code></pre>

<p><strong>Output</strong></p>

<pre><code>{'fill': 'unchanged', 'size': 'unchanged', 'angle': 'unchanged'}
</code></pre>

python dictionary