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?
If we want to make a new dictionary with selected keys removed, we can make use of dictionary comprehension
For example:
d = {
'a' : 1,
'b' : 2,
'c' : 3
}
x = {key:d[key] for key in d.keys() - {'c', 'e'}} # Python 3
y = {key:d[key] for key in set(d.keys()) - {'c', 'e'}} # Python 2.*
# x is {'a': 1, 'b': 2}
# y is {'a': 1, 'b': 2}