I\'ve got a dict that has a whole bunch of entries. I\'m only interested in a select few of them. Is there an easy way to prune all the other ones out?
dict
Here's an example in python 2.6:
>>> a = {1:1, 2:2, 3:3} >>> dict((key,value) for key, value in a.iteritems() if key == 1) {1: 1}
The filtering part is the if statement.
if
This method is slower than delnan's answer if you only want to select a few of very many keys.