a break for comprehensions

Paul Svensson paul at svensson.org
Mon Jul 30 13:19:01 EDT 2001


Greg Ewing <greg at cosc.canterbury.ac.nz> writes:

>Kevin Lacker wrote:
>> 
>> answer = [process(x) for x in my_list while is_good(x)]
>
>Looks quite nice. But what should
>
>  answer = [process(x,y) for x in a for y in b while f(x,y)]
>
>do? Should the break terminate the whole comprehension,
>or just the enclosing for-clause?

In my view, it should obviously end the whole thing; but then I
never see list comprehensions a procedural description for how
to generate the list, but rather a symbolic description of
the list content.  With that view it shouldn't matter which
for-clause gets evaluated innermost, and so the only unambigous
thing for the while clause to end, is the whole comprehension.

	/Paul




More information about the Python-list mailing list