Welcome, guest | Sign In | My Account | Store | Cart

A simple function to extract a subset of items from a dictionary.

Python, 2 lines
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)))

3 comments

Matthew Pettis 14 years, 7 months ago  # | flag

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 };

Trent Mick 13 years, 10 months ago  # | flag

More modern Python way to do this:

def extract(d, keys):
    return dict((k, d[k]) for k in keys if k in d)
Grant Jenks 9 years, 7 months ago  # | flag

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.

Created by David Benjamin on Wed, 20 Feb 2002 (PSF)
Python recipes (4591)
David Benjamin's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks