A simple function to extract a subset of items from a dictionary.
| Python |
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)), {})
|
Discussion
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)))


Comments
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 };
Sign in to comment