List transpose method

Bjorn Pettersen BPettersen at NAREX.com
Thu Nov 1 19:15:02 EST 2001


> From: Gerhard Häring [mailto:gh_pythonlist at gmx.de] 
> 
> On Thu, Nov 01, 2001 at 08:51:27PM -0200, Gustavo Niemeyer wrote:
> > > Suppose I have a list like ((1,2), (3,4), (5,6)). Can some 
> > > functional guru tell me a one liner to transpose this to the form 
> > > ((1,3,5),(2,4,6))? I can only come up with an ugly multiliner.
> > 
> > >>> zip((1,2),(3,4),(5,6))
> > [(1, 3, 5), (2, 4, 6)]
> 
> Hmm. How would I apply zip if I have a variable already, like
> 
> l = ((1,2),(3,4),(5,6))

>>> x = ((1,2),(3,4),(5,6))
>>> zip(*x)
[(1, 3, 5), (2, 4, 6)]
>>>

-- bjorn




More information about the Python-list mailing list