Efficiently Split A List of Tuples

Richard none at pacbell.net
Wed Jul 13 19:55:39 EDT 2005


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







More information about the Python-list mailing list