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>There's not really a good way to do this if you need to keep the original dictionary the same.</p>

<p>If you don't, you could <code>pop</code> the key you don't want before getting the keys.</p>

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

<p>This will mutate <code>d</code>, so again, don't use this if you need all of <code>d</code> somewhere else. You could make a copy of <code>d</code>, but at that point you're better off just iterating over a filtered copy of <code>d.keys()</code>. For example:</p>

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

python dictionary