inverse of the zip function

D

David C. Fox

Is there a function which takes a list of tuples and returns a list of
lists made up of the first element of each tuple, the second element of
each tuple, etc.?

In other words, the the inverse of the built-in zip function?

David
 
S

Simon Burton

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.
 
S

Simon Burton

OK, i think i see now. it's swapping rows<->columns, and might help this
other guy with his gridcontrols. But zip() should return (). No?

Simon.
 
R

Raymond Hettinger

In other words, the the inverse of the built-in zip function?
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
 
D

Diez B. Roggisch

Raymond said:
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:

I understand why it works as inverse when *<list> creates a argument list of
list element. But don't understand why * works that way in this context.
Does ** do this for maps and keywordargs, too? Hey, this is python - lets
try:.... pass
....
Coooool. Where is that documented? Never stumbled across it so far!

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top