[Tutor] Accesing "column" of a 2D list

Ian Witham witham.ian at gmail.com
Tue Aug 21 05:46:13 CEST 2007


This looks like a job for List Comprehensions!

>>> list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> new_list = [item[1] for item in list]
>>> new_list
[2, 5, 8]
>>>

looks good?

Ian

On 8/21/07, Orest Kozyar <orest.kozyar at gmail.com> wrote:
>
> I've got a "2D" list (essentially a list of lists where all sublists are
> of
> the same length).  The sublists are polymorphic.  One "2D" list I commonly
> work with is:
>
> [ [datetime object, float, int, float],
>   [datetime object, float, int, float],
>   [datetime object, float, int, float] ]
>
> I'd like to be able to quickly accumulate the datetime column into a list.
> Is there a simple/straightforward syntax (such as list[:][0]) to do this,
> or
> do we need to use a for loop?  I expect what I have in mind is similar to
> the Python array type, except it would be polymorphic.
>
> Thanks,
> Orest
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070821/19390a8c/attachment.htm 


More information about the Tutor mailing list