List transpose method

Don O'Donnell donod at home.com
Thu Nov 1 23:20:09 EST 2001


Emile van Sebille wrote:
> 
> "Don O'Donnell" <donod at home.com> wrote in message
> news:3BE1DD41.C58238FE at home.com...
> > Tom Harris wrote:
> > >
> > > Hi,
> > >
> > > 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.
> > >
> > >>> lst = [(1,2), (3,4), (5,6)]
> > >>> trn = apply(zip, lst)
> > >>> trn
> > [(1, 3, 5), (2, 4, 6)]
> >
> > Cheers,
> > Don
> 
> Cool.  I hadn't realized that zip could be used for more than two sequences.
> 
> --

Yep.  Although the zipper metaphor gets a bit streched.

-Don



More information about the Python-list mailing list