My Opera is closing 3rd of March

Raphael's Pflock

miscellaneous Opera, Debian, Media and German posts

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

Legalize it...Weihnachtsgeschenke

Comments

Unregistered user Wednesday, March 16, 2011 6:51:32 PM

Anonymous writes: Thanks, you saved my life :D

Unregistered user Tuesday, March 22, 2011 10:35:10 PM

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

Write a comment

New comments have been disabled for this post.