<br><br><div class="gmail_quote">On Thu, Sep 16, 2010 at 12:27 PM, Michael Powe <span dir="ltr"><<a href="mailto:michael@trollope.org">michael@trollope.org</a>></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 = ['label', 'guid']<br>
<br>
blist = ['column0label', 'column1label', 'dimension0guid',<br>
'description', 'columnid']<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't in<br>
alist (but, I've had bad experiences removing items from lists "in<br>
place," so I tend toward the "copy" 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 'for' 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>
"The secret to strong security: less reliance on secrets."<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>