[Python-Dev] PEP 201 - Parallel iteration

Gordon McMillan gmcm@hypernet.com
Tue, 18 Jul 2000 08:26:02 -0400


M.-A. Lemburg wrote:

> Moshe Zadka wrote:
> > 
> > (assume a = [1, 2, 3])
> > 
> > On Mon, 17 Jul 2000, Gordon McMillan wrote:
> > 
> > > How about a fourth: zip(a) is the same as zip(a, []) ?
> > 
> > Huh???????????????????
> 
> I think he meant: zip((a,[]))

No I didn't. Sigh.

  >>pad([1,2,3], [1], pad=None)
  [(1,1), (2,None), (3,None)]

  >>pad([1,2,3], [], pad=None)
  [(1,None), (2,None), (3,None)]
 
(If you don't agree with that, you don't agree with the PEP).

So I'm proposing that the 2nd positional arg, if not present, be 
treated as []. That makes:

  >>zip([1,2,3], pad=None)
  [(1,None), (2,None), (3,None)
  >>zip([1,2,3])
  []

The other possibility is to make Evan Simpson's dream come 
true, so
   >>zip([(1,None), (2,None), (3,None)])
   [[1,2,3],[None,None,None]]

But then you'd have to throw an exception if the (single) arg 
wasn't a rectangular sequence of sequences. Also, since 
lately Guido's on a kick to get rid of auto-tupling/untupling of 
args, I think you need to use apply(zip, tuple(...)) or the *args 
syntax to accomplish this.

> Here's another one: why should zip() have this special case
> at all ? I mean, have you ever seen a zipper with only one
> part ? Ok, there is one-hand clapping, but I wouldn't consider
> this the normal mode of operation ;-) IMHO, all the special
> casing does is mask programming errors.

Which doesn't answer the question posed in the PEP. All of 
the proposed responses are special cases. The one I've seen 
mentioned most often:
   >>zip([1,2,3])
   [(1,),(2,),(3,)]
is very much as special case, as you can see if you ask 
"what happens when I add a pad arg?".

However, if the dev list is having so much trouble deciding how 
it should decay, I'll vote for throwing an exception.
 
> And another one: zip() as defined is an operation which is -- in
> some respects -- the inverse of itself, yet the name does not
> carry this information. You can't zip a zipper twice. The
> mathematical operation behind it is called transposition (you
> view the sequence of sequences as matrix), so in that light the
> natural name would be transpose()... tranpose(transpose(matrix)))
> has the same structure as matrix itself.

But zip takes some number of sequences, not necessarily of 
the same length. That's not a matrix. I think if you go that 
route, the user has to first insure they're all the same length 
and then package them into a sequence. It might have its 
uses, but not as a replacement for map(None, ...).



- Gordon