[Python-3000] Parallel iteration syntax

Guido van Rossum guido at python.org
Tue Mar 28 20:07:13 CEST 2006


On 3/27/06, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Some years ago there was a long discussion about extending
> the for-loop to express parallel iteration over a number
> of iterables, which ended with the conclusion that such
> an extension was syntactically impossible, and the creation
> of zip().
>
> Slightly too late for consideration, I did come up with
> what I believe is a backwards-compatible syntax extension
> to support this:
>
>    for (x in iter1, y in iter2):
>      ...
>
> This is currently a syntax error, so there is no clash
> with existing semantics.
>
> I'm mentioning it here again just in case anyone wants
> to consider it for Py3k. I still believe it would be
> nice to have a direct syntax for parallel iteration
> to avoid the overhead of using zip or iterzip.
>
> Also I think the above is easier to read, because it
> puts each variable next to the relevant expression.

Based on the feedback so far I think not. There's also the issue that

  for (x in A, y in B):

could just as well be meant as a shortcut for

  for x in A:
    for y in B:

The proposed syntax doesn't quite jive with my guts, and the issue of
"what to do if they are of unequal length" is a good one, which is
better solved by being explicit and using zip (== izip).

Finally (maybe this is why it doesn't jive :-) it seems to me that
separating the variables is *worse* than

  for x, y in zip(A, B):

because the latter emphasizes that you get a new x and a new y at the same time.

--
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list