Efficiently Split A List of Tuples

Cyril Bazin cyril.bazin at gmail.com
Wed Jul 13 20:04:19 EDT 2005


if t is your data, you can use:

l1, l2 = zip(*t)

Cyril


On 7/14/05, Richard <none at pacbell.net> wrote:
> 
> 
> I have a large list of two element tuples. I want two separate
> lists: One list with the first element of every tuple, and the
> second list with the second element of every tuple.
> 
> Each tuple contains a datetime object followed by an integer.
> 
> Here is a small sample of the original list:
> 
> ((datetime.datetime(2005, 7, 13, 16, 0, 54), 315),
> (datetime.datetime(2005, 7, 13, 16, 6, 12), 313),
> (datetime.datetime(2005, 7, 13, 16, 16, 45), 312),
> (datetime.datetime(2005, 7, 13, 16, 22), 315),
> (datetime.datetime(2005, 7, 13, 16, 27, 18), 312),
> (datetime.datetime(2005, 7, 13, 16, 32, 35), 307),
> (datetime.datetime(2005, 7, 13, 16, 37, 51), 304),
> (datetime.datetime(2005, 7, 13, 16, 43, 8), 307))
> 
> I know I can use a 'for' loop and create two new lists
> using 'newList1.append(x)', etc. Is there an efficient way
> to create these two new lists without using a slow for loop?
> 
> r
> 
> 
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050714/96a60698/attachment.html>


More information about the Python-list mailing list