A simple function to extract a subset of items from a dictionary.
1 2 | extract = lambda keys, dict: reduce(lambda x, y: x.update({y[0]:y[1]}) or x,
map(None, keys, map(dict.get, keys)), {})
|
I use dictionaries for many things including database rows, primary/compound keys, and variable namespaces for template parsing. I often need to create a dictionary based on another dictionary but only containing a subset of that dictionary's items. This is the function I use to solve this problem.
Example 1:
>>> d = {'a': 5, 'b': 6, 'c': 7}
>>> extract(['a', 'b'], d)
{'b': 6, 'a': 5}
>>> d
{'b': 6, 'c': 7, 'a': 5}
Example 2:
>>> e, f, g = 1, 2, 3
>>> extract(['f', 'g'], vars())
{'f': 2, 'g': 3}
The above code has been tested in Python 1.5.2 and greater. In newer versions of Python, this function can be simplified: extract = lambda x, y: dict(zip(x, map(y.get, x)))
Works for my needs (new to Python)... surprising this doesn't have a simpler implementation in Python... in Perl, this can be done as:
my %subset_hash; @subset_hash = @oldhash{ @subset_of_keys };
More modern Python way to do this:
You could compute the subset faster if you maintained the keys in sorted order and bisected them. The sortedcontainers module provides just such an API. It implements sorted list, sorted dict, and sorted set data types in pure-Python and is fast-as-C implementations (even faster!). Learn more about sortedcontainers, available on PyPI and github.