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>Here's a little benchmark comparing a couple of good posted solutions:</p>

<pre><code>import timeit

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


def f1(d):
    return d.viewkeys() - {"inside", }


def f2(d):
    return filter(lambda x: x not in ['inside'], d.viewkeys())

N = 10000000
print timeit.timeit('f1(d)', setup='from __main__ import f1, d', number=N)
print timeit.timeit('f2(d)', setup='from __main__ import f2, d', number=N)

# 5.25808963984
# 8.54371443087
# [Finished in 13.9s]
</code></pre>

<p>Conclusion: f1 not only is better in terms of readability but also in terms of performance. In python 3.x you'd use keys() instead.</p>

<p>So I'd say @Kasramvd answer is the right one for this post</p>

python dictionary