title: Selecting dictionary items by key efficiently in Python
body: <p>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?</p>
  
  <p>for example:</p>
  
  <pre><code># a dictionary mapping strings to stuff
  mydict = {'quux': ...,
            'bar': ...,
            'foo': ...}
  
  # list of keys to be selected from mydict
  keys_to_select = ['foo', 'bar', ...]
  </code></pre>
  
  <p>The way I came up with is:</p>
  
  <pre><code>filtered_mydict = [mydict[k] for k in mydict.keys() \ 
                     if k in keys_to_select]
  </code></pre>
  
  <p>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.</p>