a break for comprehensions

phys137 at mailcity.com phys137 at mailcity.com
Fri Jul 27 00:38:50 CEST 2001


having break in list comprehensions will limit in the future possible level
of parallelism. The original idea of list comprehensions does _not_ include
the order in which the elements are evaluated. Because of that they can all
be evaluated in parallel - a compiler/interpreter can decide to do that.

Then, with side effects possible inside function calls I guess it may not
matter that much in Python.

"Marcin 'Qrczak' Kowalczyk" <qrczak at knm.org.pl> wrote in message
news:slrn.pl.9m0ej2.o3q.qrczak at qrnik.zagroda...
> Thu, 26 Jul 2001 10:44:55 -0400, Kevin Lacker <kdl4 at acpub.duke.edu> pisze:
>
> > Can you do this:
> >
> > answer = []
> > for x in my_list:
> >     if not is_good(x):
> >         break
> >     answer.append(process(x))
> >
> > with a list comprehension somehow?
>
> The pattern can be wrapped in a function. It's quite nice in Python 2.2:
>
> def take_while(p, l):
>     for x in l:
>         if not p(x): break
>         yield x
>
> answer = [process(x) for x in take_while(is_good, my_list)]
>
> > I think it would be cool to be able to reuse the while keyword
> > inside comprehensions like
> >
> > answer = [process(x) for x in my_list while is_good(x)]
>
> Looks nice for me.
>
> --
>  __("<  Marcin Kowalczyk * qrczak at knm.org.pl http://qrczak.ids.net.pl/
>  \__/
>   ^^                      SYGNATURA ZASTÊPCZA
> QRCZAK





More information about the Python-list mailing list