By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

inverse of the zip function

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Terry Reedy wrote:
"David C. Fox" <da*******@post.harvard.edu> wrote in message
news:9sBVa.12998$o%2.6289@sccrnsc02...
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?

Go to
http://groups.google.com/groups?hl=e...mp.lang.python
enter 'zip inverse', and check search Python only.

TJR


Thanks. I've gotten so used to reading this group with Mozilla Mail
that I forgot about google groups.

David

Jul 18 '05 #2

P: n/a
On Tue, 29 Jul 2003 22:06:20 +0000, Raymond Hettinger wrote:
"David C. Fox" <da*******@post.harvard.edu> wrote in message
news:9sBVa.12998$o%2.6289@sccrnsc02...
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?


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.

Jul 18 '05 #3

P: n/a
On Wed, 30 Jul 2003 08:31:47 +1000, Simon Burton wrote:
On Tue, 29 Jul 2003 22:06:20 +0000, Raymond Hettinger wrote:
"David C. Fox" <da*******@post.harvard.edu> wrote in message
news:9sBVa.12998$o%2.6289@sccrnsc02...
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?


When used with the * operator, zip() is its own inverse:


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.

Jul 18 '05 #4

P: n/a
> >> 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

Jul 18 '05 #5

P: n/a
Raymond Hettinger wrote:
>> In other words, the the inverse of the built-in zip function?
>
> When used with the * operator, zip() is its own inverse: 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:
def foo(a=None, b=None): .... pass
.... foo(a=10, b=20)
foo(**{'a':10, 'b':20})


Coooool. Where is that documented? Never stumbled across it so far!

Diez
Jul 18 '05 #6

P: n/a
> 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:
def foo(a=None, b=None): ... pass
... foo(a=10, b=20)
foo(**{'a':10, 'b':20})


Coooool. Where is that documented? Never stumbled across it so far!


http://www.python.org/dev/doc/devel/ref/calls.html
Raymond Hettinger
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.