Select column from a list

Alan G Isaac alan.isaac at gmail.com
Sat Aug 29 05:36:12 CEST 2009


On 8/28/2009 3:45 AM hoffik wrote:
> 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.

Not quite what you asked but ...

>>> rows = [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
>>> cols = map(None,*rows)
>>> cols
[(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)]

Now you can have any "column" you want.

Alan Isaac

PS You can also use imap if you prefer not to
create the list.



More information about the Python-list mailing list