Posted By

kristi on 02/20/11


Tagged

filter from keys dict


Versions (?)

Filter a dict based on a list of keys


 / Published in: Python
 

  1. # Filter a dict
  2. def from_keys(d, iterator):
  3. return dict((i, d[i]) for i in iterator)
  4.  
  5. mydict = {'apple':'red', 'banana':'yellow', 'orange':'purple'}
  6.  
  7. print from_keys(mydict, 'apple orange'.split())
  8. # {'orange': 'purple', 'apple': 'red'}

Report this snippet  

You need to login to post a comment.