interchanging rows and columns

Rimbault Jean-Claude rimbault at cybercable.fr
Sat Oct 14 06:12:13 EDT 2000


Johannes Zellner wrote:

> how can I turn
>    [[1, 2, 3], [4, 5, 6]]
> into
>    [[1, 4], [2, 5], [3, 6]]
>
> ?
> must be easy, but I couldn't manage it.
> I tried some (map|apply) variants of the tutorial. No luck.
>
> --
>    Johannes

>>> a = [[1,2,3], [4,5,6]]
>>> apply(map, (None,)+ tuple(a))
[(1, 4), (2, 5), (3, 6)]

In Python 2.0, there is a new builtin function zip:

>>> apply(zip, a)
[(1, 4), (2, 5), (3, 6)]

Note that this will give you a list of tuples.
If you really want a list of lists, do:

>>> map(list, apply(zip, a))
[[1, 4], [2, 5], [3, 6]]




More information about the Python-list mailing list