[Python-ideas] More power in list comprehensions with the 'as' keyword

Josiah Carlson josiah.carlson at gmail.com
Thu Aug 28 16:28:50 CEST 2008


-1 on the feature, I use the compound expression as below, only have
the internal item be a generator expression to reduce peak memory
usage.  Also, the != condition is unnecessary.

[t for t in (t.strip() for t in text.split('\n')) if t]

Overloading 'as', 'with', etc., when there are simple expressions that
already do the *exact* same thing is silly.

Never mind that not everything needs to be done in a 1-liner or list
comprehension.

 - Josiah

On Wed, Aug 27, 2008 at 10:30 AM, Bruce Leban <bruce at leapyear.org> wrote:
> [t for t in [t.strip() for t in text.split('\n')] if t != '']
>
>
> On Wed, Aug 27, 2008 at 10:00 AM, Imri Goldberg <lorgandon at gmail.com> wrote:
>>
>> +1 from me, I find myself doing that all the time as well.
>>
>> On Wed, Aug 27, 2008 at 7:51 PM, Tarek Ziadé <ziade.tarek at gmail.com>
>> wrote:
>>>
>>> Hello
>>>
>>> There's a pattern I am doing all the time: filtering out some elements of
>>> a list, and cleaning them in the same move.
>>>
>>> For example, if I have a multi line text, where I want to:
>>>
>>> - keep non empty lines
>>> - clean non empty lines
>>>
>>> I am doing:
>>>
>>>     >>> text = """
>>>     ... this is a multi-line text\t
>>>     ...
>>>     ... \t\twith
>>>     ...
>>>     ... muliple lines."""
>>>
>>>     >>> [l.strip() for l in text.split('\n') if l.strip() != '']
>>>     ['this is a multi-line text', 'with', 'muliple lines.']
>>>
>>> It is not optimal, because I call strip() twice. I could use ifilter then
>>> imap or even use a real loop, but I
>>> want my simple, concise, list comprehension !  And I couldn't find a
>>> simple way to express it.
>>>
>>> The pattern can be generically resumed like this :
>>>
>>>    [transform(e) for e in seq if some_test(transform(e))]
>>>
>>> So what about using the 'as' keyword to extend lists comprehensions, and
>>> to avoid calling transform() twice ?
>>>
>>> Could be:
>>>
>>>    [transform(e) as transformed for e in seq if some_test(transformed)]
>>>
>>> In my use case I would simply have to write;:
>>>
>>>   [l.strip() as stripped for l in text.split('\n') if stripped != '']
>>>
>>> Which seems to me clear and concise.
>>>
>>> Regards,
>>> Tarek
>>>
>>> --
>>> Tarek Ziadé | Association AfPy | www.afpy.org
>>> Blog FR | http://programmation-python.org
>>> Blog EN | http://tarekziade.wordpress.com/
>>>
>>> _______________________________________________
>>> Python-ideas mailing list
>>> Python-ideas at python.org
>>> http://mail.python.org/mailman/listinfo/python-ideas
>>>
>>
>>
>>
>> --
>> Imri Goldberg
>> --------------------------------------
>> www.algorithm.co.il/blogs/
>> www.imri.co.il
>> --------------------------------------
>> -- insert signature here ----
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/mailman/listinfo/python-ideas
>>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>



More information about the Python-ideas mailing list