a break for comprehensions
Peter Hansen
peter at engcorp.com
Thu Jul 26 18:57:23 EDT 2001
Kevin Lacker wrote:
>
> 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? I'm starting to dislike explicit for
> loops just for the purposes of constructing another list from a current one.
I can clearly understand what the above code is doing. Doing this
as a list comprehension would be less readable for me (I'm not a
heavy user of list comprehensions, yet.)
Are you sure you really don't like the explicit, readable code
above, and would prefer a more compressed, (admittedly explicit,
I suppose), more cryptic version instead? There are other
languages which could do the above in one tiny line, no doubt.
(No names. :)
--
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com
More information about the Python-list
mailing list