Proposal: [... for ... while cond(x)]
Terry Reedy
tjreedy at udel.edu
Tue Aug 8 16:33:21 EDT 2006
"Eighty" <eightyx at gmail.com> wrote in message
news:1155064682.499991.246170 at h48g2000cwc.googlegroups.com...
>
> Eighty wrote:
>> I suggest a new extension of the list comprehension syntax:
>>
>> [x for x in xs while cond(x)]
This does not work.
e(x) for x in xs if cond(x)
is an abbreviation of
for x in xs:
if cond(x)
yield e(x)
and similarly with more for and if clauses,
whereas the analogous expansion of your proposal
for x in xs:
while cond(x):
yield e(x)
is an infinite loop and not at all what you mean.
>> which would be equivalent to
>>
>> list(itertools.takewhile(cond, xs))
And what would
x for x in xs while cond(x) if blah(x)
x for x in xs if blah(x) while cond(x)
x*y for x in xs while cond(x) for y in ys
mean? The ability to mix and match clauses after the first for clause is
an important part of comprehension syntax.
>> + "Takewhile operations" occur often, at least for me
So keep using the function provided. I am pretty sure takewhile is rare
for most other people.
> So does no one have a comment on this?
Ain't gonna happen.
> The one objection I can come up with
> is that this would change the set builder notation semantics too much
Yes, to the point where you are hijacking the syntax, for no useful gain,
more than extending it ;-).
Terry Jan Reedy
More information about the Python-list
mailing list