views:

78

answers:

1

suppose I have a dictionary whose keys are strings. How can I efficiently make a new dictionary from that which contains only the keys present in some list?

for example:

# a dictionary mapping strings to stuff
mydict = {'quux': ...,
          'bar': ...,
          'foo': ...}

# list of keys to be selected from mydict
keys_to_select = ['foo', 'bar', ...]

The way I came up with is:

filtered_mydict = [mydict[k] for k in mydict.keys() \ 
                   if k in keys_to_select]

but I think this is highly inefficient because: (1) it requires enumerating the keys with keys(), (2) it requires looking up k in keys_to_select each time. at least one of these can be avoided, I would think. any ideas? I can use scipy/numpy too if needed.

+9  A: 
dict((k, mydict[k]) for k in keys_to_select)

if you know all the keys to select are also keys in mydict; if that's not the case,

dict((k, mydict[k]) for k in keys_to_select if k in mydict)
Alex Martelli
When using "if k in mydict" like this, does Python perform a has_key type lookup, or does it convert the keys into a list/iterable and loop over them? If it's the latter, " if mydict.has_key(k)" might be more efficient, no? (Haven't found any documentation to clarify, either way, yet. Google skills failed me).
pycruft
To answer my own question, using "k in dict" is identical to using "dict.has_key(k)" according to http://www.python.org/dev/peps/pep-0234/
pycruft
@pycruft, yep, and, as `timeit` cqn confirm (`python -mtimeit -s'd=dict.fromkeys(range(99))' '23 in d'` etc), `in` is, in fact, about twice as fast as `has_key` (saves a named-attribute lookup each time, which is about the same operation as a dict loolup). There is never any reason to use `has_key` any more, and it's been removed from Python 3.
Alex Martelli