<br><br><div class="gmail_quote">On Thu, Sep 16, 2010 at 12:27 PM, Michael Powe <span dir="ltr">&lt;<a href="mailto:michael@trollope.org">michael@trollope.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Hello,<br>
<br>
I have two lists.<br>
<br>
alist = [&#39;label&#39;, &#39;guid&#39;]<br>
<br>
blist = [&#39;column0label&#39;, &#39;column1label&#39;, &#39;dimension0guid&#39;,<br>
&#39;description&#39;, &#39;columnid&#39;]<br>
<br>
I want to iterate over blist and extract the items that match my<br>
substrings in alist; alternatively, throw out the items that aren&#39;t in<br>
alist (but, I&#39;ve had bad experiences removing items from lists &quot;in<br>
place,&quot; so I tend toward the &quot;copy&quot; motif.)<br>
<br>
In real life, blist column entries could have embedded column numbers<br>
from 0 to 19.<br>
<br>
I can do this with excrutiatingly painful &#39;for&#39; loops.  Looking for<br>
something more efficient and elegant.<br>
<br>
Thanks.<br>
<br>
mp<br>
<font color="#888888"><br>
--<br>
Michael Powe            <a href="mailto:michael@trollope.org">michael@trollope.org</a>            Naugatuck CT USA<br>
<br>
&quot;The secret to strong security: less reliance on secrets.&quot;<br>
-- Whitfield Diffie<br>
</font><br>_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br></blockquote></div><br><div>Michel,</div><div><br></div><div>One solution is to use list comprehensions. </div><div><br></div><div>newlist = [x for x in blist if [a for a in alist if a in x]]</div><div><br></div><div>

This works, although there may be more efficient ways to accomplish this</div><div><br></div><div>Vince</div><div><br></div>