Return to Snippet

Revision: 41590
at February 20, 2011 18:32 by kristi


Initial Code
# Filter a dict
def from_keys(d, iterator):
    return dict((i, d[i]) for i in iterator)

mydict = {'apple':'red', 'banana':'yellow', 'orange':'purple'}

print from_keys(mydict, 'apple orange'.split())
# {'orange': 'purple', 'apple': 'red'}

Initial URL


Initial Description


Initial Title
Filter a dict based on a list of keys

Initial Tags
filter

Initial Language
Python