Half-baked idea: list comprehensions with "while"

Chris Rebert clp2 at rebertia.com
Thu Apr 26 13:10:05 EDT 2012


On Thu, Apr 26, 2012 at 10:02 AM, Roy Smith <roy at panix.com> wrote:
> I'm not seriously suggesting this as a language addition, just an interesting idea to simplify some code I'm writing now:
>
> x = [a for a in iterable while a]
>
> which equates to:
>
> x = []
> for a in iterable:
>    if not a:
>        break
>    x.append(a)

Someone borrowed Guido's time machine:
http://docs.python.org/library/itertools.html#itertools.takewhile

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list