Half-baked idea: list comprehensions with "while"

Ian Kelly ian.g.kelly at gmail.com
Thu Apr 26 18:02:32 EDT 2012


On Thu, Apr 26, 2012 at 11:57 AM, Kiuhnm
<kiuhnm03.4t.yahoo.it at mail.python.org> wrote:
> On 4/26/2012 19:48, Paul Rubin wrote:
>>
>> Roy Smith<roy at panix.com>  writes:
>>>
>>> x = [a for a in iterable while a]
>>
>>
>> from itertools import takewhile
>>
>> x = takewhile(bool, a)
>
>
> I see that as a 'temporary' solution, otherwise we wouldn't need 'if' inside
> of list comprehensions either.

The "if" clause is much more commonly used than "while" would be.  I
don't have any objections to the suggestion from a technical or
syntactical perspective.  I do however question whether the utility is
great enough to be worthwhile from a long-term maintenance
perspective.



More information about the Python-list mailing list