[Python-ideas] while conditional in list comprehension ??

Joao S. O. Bueno jsbueno at python.org.br
Tue Jan 29 13:35:22 CET 2013


On 29 January 2013 09:51, yoav glazner <yoavglazner at gmail.com> wrote:
> Here is very similar version that works (tested on python27)
>>>> def stop():
> next(iter([]))
>
>>>> list((i if i<50 else stop()) for i in range(100))
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
> 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
> 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]

Great. I think this nails it. It is exactly the intended behavior,
and very readable under current language capabilities.

One does not have to stop and go read what "itertools.takewhile" does,
and mentally unfold the lambda guard expression - that is what makes
this (and the O.P. request)  more readable than using takewhile.

Note: stop can also just explictly raise StopIteration -
or your next(iter([])) expression can be inlined within the generator.

It works in Python 3 as well - though for those who did not test:
it won't work for list, dicr or set  comprehensions - just for
generator expressions.


 js
-><-



More information about the Python-ideas mailing list