<div dir="ltr"><br><div class="gmail_quote">On Wed, Aug 27, 2008 at 12:51 PM, Tarek Ziadé <span dir="ltr"><<a href="mailto:ziade.tarek@gmail.com">ziade.tarek@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div dir="ltr">Hello<br><br>There's a pattern I am doing all the time: filtering out some elements of a list, and cleaning them in the same move.<br><br>For example, if I have a multi line text, where I want to:<br><br>
- keep non empty lines<br>- clean non empty lines<br><br>I am doing:<br><br> >>> text = """<br> ... this is a multi-line text\t<br> ... <br> ... \t\twith<br> ... <br> ... muliple lines."""<br>
<br> >>> [l.strip() for l in text.split('\n') if l.strip() != '']<br> ['this is a multi-line text', 'with', 'muliple lines.']<br><br>It is not optimal, because I call strip() twice. I could use ifilter then imap or even use a real loop, but I<br>
want my simple, concise, list comprehension ! And I couldn't find a simple way to express it.<br><br>The pattern can be generically resumed like this :<br><br> [transform(e) for e in seq if some_test(transform(e))]<br>
<br>So what about using the 'as' keyword to extend lists comprehensions, and<br>to avoid calling transform() twice ? <br> <br>Could be: <br><br> [transform(e) as transformed for e in seq if some_test(transformed)]</div>
</blockquote><div><br>
-1; not general enough to justify the extra overhead (both in human
mental effort and compiler changes), given that the current
alternatives (esp. genexps) are already quite readable and more flexible.<br>
<br>
George<br><br>
</div></div></div>