Transposing Python Arrays

Transposing an array (a list of lists) in Python can be easily done using the map() function.

>>> lst=[[1,2,3],[4,5,6],[7,8,9,10]]
>>>  print(list(map(lambda *x:x, *lst)))
[(1, 4, 7), (2, 5, 8), (3, 6, 9), (None, None, 10)]

This works with Python 2.x and 3.x

Originally found at Muffin Research Labs, modified for Python 3 thanks to a blog comment (see below).

Previous Post
Leave a comment

2 Comments

  1. anonymous

     /  March 16, 2011

    Anonymous writes:Thanks, you saved my life😀

    Reply
  2. anonymous

     /  March 22, 2011

    Anonymous writes:Note that in Python 3 this throws an error: None is not callable.Instead use print(list(map(lambda *x:x, *list)))

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: