What is the slickest way to transpose a square list of lists (tuple of tuples)?

Bengt Richter bokr at oz.net
Sun Jan 8 17:36:24 EST 2006


On Sun, 08 Jan 2006 15:21:59 -0600, Brian van den Broek <broek at cc.umanitoba.ca> wrote:

>Gerard Brunick said unto the world upon 08/01/06 01:27 PM:
>> My way is ugly.  These has to be a better way.
>> 
>> Thanks,
>> Gerard
>
>If you'd posted your way, I might well have seen if I could do it in a 
>nicer fashion. But, since for all I know, my best efforts would result 
>in the approach you already have, I'm unlikely to put the effort in.
>
>I suspect I'm not alone. You might do well to show the approach you 
>are unhappy with.
>
OTOH, he's probably just trying to get someone to remember zip-abuse for him.
Hope it's not homework ;-)

 >>> sq = [[r+c for c in 'abc'] for r in '123']
 >>> for r in sq: print r
 ...
 ['1a', '1b', '1c']
 ['2a', '2b', '2c']
 ['3a', '3b', '3c']
 >>> for r in (zip(*sq)): print r
 ...
 ('1a', '2a', '3a')
 ('1b', '2b', '3b')
 ('1c', '2c', '3c')


Regards,
Bengt Richter



More information about the Python-list mailing list