Filtering a list based on a second list
<p>Here is what is probably a simple question, but I wasn't able to find a straightforward answer for on my own.</p>

<p>Given two lists, one with only a list of ids, the other with all data, including some ids that we don't care about:
<br><code>all_data = [['abc', 123], ['cde', 234], ['fgh', 345]]</code>
<br> <code>ids  = ['abc', 'fgh']</code></p>

<p>what is the best way to get the following output, note that it keeps only those that have the same ids:
<code>new_data = [['abc', 123], ['fgh', 345]]</code></p>

<p>My current code does something like:</p>

<pre><code>for x in all_data:
    for y in ids:
         if x[0] == y:
              new_data.append(x)
</code></pre>

<p>What woud you do differently? Is there a built-in function that takes care of this that I missed somewhere?</p>

<p>(I say "something like" because it's actually a very long sequence involving sets and all that which is why there is not "pythonic" one-liner to share.)</p>

<p>UPDATE:
Well you guys are fun.</p>

<p>How about I make it a little harder. What if instead of "all_data" I have a a dictionary all_data_dict that has several list entries of the same format as "all_data"? Following the rules, I'll make sure to accept the answer to the original question, but if you all want to keep up with the fun, let's see what we get!</p>
