Select column from a list

Anthra Norell anthra.norell at bluewin.ch
Fri Aug 28 05:59:17 EDT 2009


Vlastimil Brom wrote:
> 2009/8/28 hoffik <beorn at seznam.cz>:
>   
>> Hello,
>>
>> I'm quite new in Python and I have one question. I have a 2D matrix of
>> values stored in list (3 columns, many rows). I wonder if I can select one
>> column without having to go through the list with 'for' command.
>> ...
>>     
>
> I guess, it won't be possible without an explicit or implicit loop;
> you may try:
>
>   
>>>> from operator import itemgetter
>>>> nested_list = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]
>>>> map(itemgetter(1), nested_list)
>>>>         
> [2, 20, 200]
>   
>
> vbr
>   
How about rotating the list with zip?
 >>> zip (*nested_list)[1]
(2, 20, 200)

Frederic




More information about the Python-list mailing list