How to generically transform a list?

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Fri Aug 27 05:26:04 EDT 2004


Roel Schroeven wrote:

>> Just out of pure curiosity: Is there a langue that allows vertical and 
>> horizontal slicing and dicing with the same built-in pattern?
> 
> 
> You can do it (sorta) in Python: use zip to turn the columns into rows 
> and vice-versa, apply the slicing, than zip back:
> 
>  >>> l = [['a', 1, 11, 'aa'], ['b', 2, 22, 'bb'], ['c', 3, 33, 'cc']]
>  >>> zip(*(zip(*l)[2:0:-1]))
> [(11, 1), (22, 2), (33, 3)]

You end up with a list of tuples instead of a list of lists though.

-- 
"Codito ergo sum"
Roel Schroeven



More information about the Python-list mailing list