

zip is its own inverse - emmett

I found myself, for fairly silly reasons, desiring to find the inverse to zip() today. After some thought, I realized zip is its own inverse:<p><pre><code>  &#62;&#62;&#62; def doublezip(xs):
  ...   print zip(*zip(*xs))
  ... 
  &#62;&#62;&#62; doublezip([[9, 8, 7, 6], [1, 2, 3, 4], [4, 5, 6, 7]])
  [(9, 8, 7, 6), (1, 2, 3, 4), (4, 5, 6, 7)]
</code></pre>
This struck me very interesting. I'm not sure I could ever explain why though.
======
iamwil
That's kinda interesting, though if you just work it out on paper, you can see
why.

zip basically groups all the elements with the same index together (like a dot
product, except you don't multiply or sum). So all the elements from the first
array will be in the array of the first resulting zip.

    
    
      zip([a0, a1,..,an], [b0, b1,..,bn]) =>
        [[a0, b0], [a1, b1], ..., [an, bn]]
    

And if you look at it, of all the resulting arrays of the first zip, all the
ones in the first index of every array came from the first array. all the ones
in the 2nd index of every array came from the 2nd array. If you run a 2nd zip,
you're just, once again, collecting all the elements of all arrays with the
same index, which reverts it back to what it was before.

Looking at it another way, if you think about it as a matrix, you'd have, say
a bunch of row vectors:

    
    
      [[1,2,3,4],
       [5,6,7,8]]
    

If you run the first zip, you'd just get a bunch of column vectors (excuse the
ascii, can't realy make column vectors:

    
    
      [[1  [2  [3  [4
        5], 6], 7]  8]]
    

And if you run zip again, you just get back row vectors.

~~~
emmett
Aha!

zip == matrix transpose.

I wonder why I never noticed that before...

~~~
iamwil
yeah, I never noticed it until you posted this and I worked it out on paper.

when I first saw zip, I thought, when would I use that? Then when I was
playing around with erlang trying to write a dot product function, that's when
I figured it out.

------
aston
Zip = Matrix Transpose is on Peter Norvig's list of Infrequently Answered
Questions about Python.

------
ramen
zip is matrix transposition in disguise.

