inverse of the zip function

Discussion in 'Python' started by David C. Fox, Jul 29, 2003.

1. David C. FoxGuest

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

David C. Fox, Jul 29, 2003

2. David C. FoxGuest

Terry Reedy wrote:

> "David C. Fox" <> 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
> enter 'zip inverse', and check search Python only.
>
> TJR
>
>

Thanks. I've gotten so used to reading this group with Mozilla Mail

David

David C. Fox, Jul 29, 2003

3. Simon BurtonGuest

On Tue, 29 Jul 2003 22:06:20 +0000, Raymond Hettinger wrote:

> "David C. Fox" <> 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.

Simon Burton, Jul 29, 2003
4. Simon BurtonGuest

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

Simon Burton, Jul 29, 2003
5. Raymond HettingerGuest

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

Raymond Hettinger, Jul 29, 2003
6. Diez B. RoggischGuest

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

Diez B. Roggisch, Jul 30, 2003
7. Raymond HettingerGuest

> 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

Raymond Hettinger, Jul 30, 2003