title: Python dictionary comprehension very slow
body: <p>I have a dictionary <code>d1</code> and a list <code>l1</code>.</p>
  
  <p>The dictionary keys are strings, and the values are Objects I have defined myself. If it helps, I can describe the Object in more detail but for now, the objects have a list attribute <code>names</code>, and some of the elements of <code>name</code> may or may not appear in <code>l1</code>. </p>
  
  <p>What I wanted to do was to throw away any element of the dictionary <code>d1</code>, in which the <code>name</code> attribute of the object in said element does not contain any of the elements that appear in <code>l1</code>.</p>
  
  <p>As a trivial example:</p>
  
  <pre><code>l1 = ['cat', 'dog', 'mouse', 'horse', 'elephant', 
        'zebra', 'lion', 'snake', 'fly']
  
  d1 = {'1':['dog', 'mouse', 'horse','orange', 'lemon'],
        '2':['apple', 'pear','cat', 'mouse', 'horse'], 
        '3':['kiwi', 'lime','cat', 'dog', 'mouse'], 
        '4':['carrot','potato','cat', 'dog', 'horse'], 
        '5':['chair', 'table', 'knife']}
  </code></pre>
  
  <p>so the resulting dictionary will be more or less the same but the elements of each list will be the key-value pairs from <code>1</code> to <code>4</code> excluding the fruit and vegetables, and will not contain a 5th key-value par as none of the furniture values appear in <code>l1</code>.</p>
  
  <p>To do this I used a nested list/dictionary comprehension which looked like this:</p>
  
  <pre><code>d2 = {k: [a for a in l1 if a in d1[k]] for k in d1.keys()}
  print(d2)
  
  &gt;&gt;&gt;&gt;{'1': ['dog', 'mouse', 'horse'], 
       '3': ['cat', 'dog', 'mouse'], 
       '2': ['cat', 'mouse', 'horse'], 
       '5': [], 
       '4': ['cat', 'dog', 'horse']}
  
  d2 = {k: v for k,v in d2.iteritems() if len(v)&gt;0}
  print(d2)
  
  &gt;&gt;&gt;&gt;{'1': ['dog', 'mouse', 'horse'], 
       '3': ['cat', 'dog', 'mouse'], 
       '2': ['cat', 'mouse', 'horse'],  
       '4': ['cat', 'dog', 'horse'],}
  </code></pre>
  
  <p>This seems to work, but for big dictionaries, 7000+ items, it takes around 20 seconds to work through. In and of itself, not horrible, but I need to do this inside a loop that will iterate 10,000 times, so currently it's not feasible. Any suggestions on how to do this quickly?</p>