<div dir="ltr">+1 from me, I find myself doing that all the time as well.<br><br><div class="gmail_quote">On Wed, Aug 27, 2008 at 7: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)]<br>

<br>In my use case I would simply have to write;:<br><br>  [l.strip() as stripped for l in text.split('\n') if stripped != '']<br><br>Which seems to me clear and concise.<br><br>Regards,<br>Tarek<br><br>-- <br>

Tarek Ziadé | Association AfPy | <a href="http://www.afpy.org" target="_blank">www.afpy.org</a><br>Blog FR | <a href="http://programmation-python.org" target="_blank">http://programmation-python.org</a><br>Blog EN | <a href="http://tarekziade.wordpress.com/" target="_blank">http://tarekziade.wordpress.com/</a><br>


</div>
<br>_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>Imri Goldberg<br>--------------------------------------<br><a href="http://www.algorithm.co.il/blogs/">www.algorithm.co.il/blogs/</a><br><a href="http://www.imri.co.il">www.imri.co.il</a><br>
--------------------------------------<br>-- insert signature here ----<br>
</div>