Filter a Dictionary by filter() Instead of creating our own function we can also use python's filter() function too. filter() function accepts a, an iterable sequence to be filtered. a function that accepts an argument and returns bool i.e. True or False based on it's logic.
If you only need the dictionary values -0.3246 , -0.9185 , and -3985 use: your_dict. values() . If you want both keys and values use: your_dict. items() which returns a list of tuples [(key1, value1), (key2, value2), ...] .
With CPython 2.7, using dict() to create dictionaries takes up to 6 times longer and involves more memory allocation operations than the literal syntax. Use {} to create dictionaries, especially if you are pre-populating them, unless the literal syntax does not work for your case.
Python dictionary method len() gives the total length of the dictionary. This would be equal to the number of items in the dictionary.
Constructing a new dict:
dict_you_want = { your_key: old_dict[your_key] for your_key in your_keys }
Uses dictionary comprehension.
If you use a version which lacks them (ie Python 2.6 and earlier), make it dict((your_key, old_dict[your_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_dict
s 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(keys) - set(your_dict)
for unwanted_key in unwanted: del your_dict[unwanted_key]
Slightly more elegant dict comprehension:
foodict = {k: v for k, v in mydict.items() if k.startswith('foo')}
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.
You can do that with project function from my funcy library:
from funcy import project
small_dict = project(big_dict, keys)
Also take a look at select_keys.
Code 1:
dict = { key: key * 10 for key in range(0, 100) }
d1 = {}
for key, value in dict.items():
if key % 2 == 0:
d1[key] = value
Code 2:
dict = { key: key * 10 for key in range(0, 100) }
d2 = {key: value for key, value in dict.items() if key % 2 == 0}
Code 3:
dict = { key: key * 10 for key in range(0, 100) }
d3 = { key: dict[key] for key in dict.keys() if key % 2 == 0}
All pieced of code performance are measured with timeit using number=1000, and collected 1000 times for each piece of code.
For python 3.6 the performance of three ways of filter dict keys almost the same. For python 2.7 code 3 is slightly faster.
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.
Given your original dictionary orig
and the set of entries that you're interested in keys
:
filtered = dict(zip(keys, [orig[k] for k in keys]))
which isn't as nice as delnan's answer, but should work in every Python version of interest. It is, however, fragile to each element of keys
existing in your original dictionary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With