Filter dict to contain only certain keys?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island Looping
--
Chapters
00:00 Question
00:24 Accepted answer (Score 915)
01:25 Answer 2 (Score 229)
01:38 Answer 3 (Score 74)
02:02 Answer 4 (Score 30)
02:21 Thank you
--
Full question
https://stackoverflow.com/questions/3420...
Answer 3 links:
[project]: http://funcy.readthedocs.org/en/latest/c...
[funcy]: https://github.com/Suor/funcy
[select_keys]: http://funcy.readthedocs.org/en/latest/c...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #dictionary
#avk47
ACCEPTED ANSWER
Score 988
Constructing a new dict:
dict_you_want = {key: old_dict[key] for key in your_keys}
Uses dictionary comprehension.
If you use a version which lacks them (ie Python 2.6 and earlier), make it dict((key, old_dict[key]) for ...). It's the same, though uglier.
Note that this, unlike jnnnnn's version, has stable performance (depends only on number of your_keys) for old_dicts of any size. Both in terms of speed and memory. Since this is a generator expression, it processes one item at a time, and it doesn't looks through all items of old_dict.
Removing everything in-place:
unwanted = set(old_dict) - set(your_keys)
for unwanted_key in unwanted: del your_dict[unwanted_key]
ANSWER 2
Score 267
Slightly more elegant dict comprehension:
foodict = {k: v for k, v in mydict.items() if k.startswith('foo')}
ANSWER 3
Score 76
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.
This method is slower than delnan's answer if you only want to select a few of very many keys.
ANSWER 4
Score 29
This one liner lambda should work:
dictfilt = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y) ])
Here's an example:
my_dict = {"a":1,"b":2,"c":3,"d":4}
wanted_keys = ("c","d")
# run it
In [10]: dictfilt(my_dict, wanted_keys)
Out[10]: {'c': 3, 'd': 4}
It's a basic list comprehension iterating over your dict keys (i in x) and outputs a list of tuple (key,value) pairs if the key lives in your desired key list (y). A dict() wraps the whole thing to output as a dict object.