inverse of the zip function

Raymond Hettinger vze4rx4y at
Wed Jul 30 00:59:14 CEST 2003

> >> In other words, the the inverse of the built-in zip function?
> >
> > When used with the * operator, zip() is its own inverse:
> >
> This (obviously) doesn't work when z has length 0 or 2.
> I don't quite understand why zip is overloaded ...
> Oh, hang on, it does work for length 2! that's neat-o,
> and perhaps that's why zip was extended. Is it a functional programming
> convention, i wonder.
> Simon.

There is no special extension to zip().
It just happens to be one of those functions
like int.__neg__() that is closely related to
its own inverse.

* or apply() serve only to break a list into
individual arguments.  So, transpose() can
be defined like this:

def transpose(mat):
    return zip(*mat)

The transpose() is its own inverse for rectangular
matrices represented as lists of tuples.

Raymond Hettinger

More information about the Python-list mailing list