a potential pep to extend the syntax of for loops

pang pablo.angulo at uam.es
Mon Mar 9 09:35:18 EDT 2009


>>>> for x in range(10) for y in range(10) if x+y==5:
>>>>     print x,y

> What is that supposed to mean? Nested looping? Why is that (confusing
> thing) better than:
>
> from itertools import product
>
> for x, y in product(range(10), range(10)) if x + y == 5:
>      print x, y
>

That confusing thing is what you use every day for list and generator
comprehension.

But I didn't know that python 2.6 incorporated itertools.product (I
use 2.5, and didn't search hard enough)

I agree that itertools.product is a perfectly nice solution

> +1 on the "if" and (possibly) "while" conditional looping.
> -1 on shorthand nested for.

same thing here, now that I know about itertools.product



More information about the Python-list mailing list