[docs] transpose of a matrix represented with nested lists

Wacek Kusnierczyk waku at idi.ntnu.no
Sat Aug 28 07:57:58 CEST 2010


  Hello,

In section 5.1 [1] of Python 3 online documentation, you discuss ways to 
transpose a matrix represented as a nested list.  The following example 
is given:

mat = [
...        [1, 2, 3],
...        [4, 5, 6],
...        [7, 8, 9],
...       ]

list(zip(*mat))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]


That's correct only if further code ignores the distinction between 
tuples and lists.  In the code as it is, the 'list' part is actually 
redundant, as zip will return a list anyway.

To transpose a matrix using zip, the following seems more accurate:

map(list, zip(*m))


Regards,
vQ

[1] 
http://docs.python.org/py3k/tutorial/datastructures.html#nested-list-comprehensions


More information about the docs mailing list