EDIT: Here's some actual numbers! The izip
, list comprehension, and numpy
ways of doing this are all about the same speed.
# zip
>>> timeit.timeit( "newlist = zip(*someList)[0]", setup = "someList = [range(1000000), range(1000000), range(1000000)]", number = 10 )
1.4984046398561759
# izip
>>> timeit.timeit( "newlist = izip(*someList).next()", setup = "someList = range(1000000), range(1000000), range(1000000)]; from itertools import izip", number = 10 )
2.2186223645803693e-05
# list comprehension
>>> timeit.timeit( "newlist = [li[0] for li in someList]", setup = "someList = [range(1000000), range(1000000), range(1000000)]", number = 10 )
1.4677040212518477e-05
# numpy
>>> timeit.timeit( "newlist = someList[0,:]", setup = "import numpy as np; someList = np.array([range(1000000), range(1000000), range(1000000)])", number = 10 )
6.6217344397045963e-05
>>>
For large data structures like this you should use numpy
, which implementes an array type in C and hence is significantly more efficient. It also provides all the matrix manipulation you will ever want.
>>> import numpy as np
>>> foo = np.array([[0,1,2],[3,4,5],[6,7,8]])
>>> foo[:,0]
array([0, 3, 6])
You can also transpose...
>>> foo.transpose()
array([[0, 3, 6],
[1, 4, 7],
[2, 5, 8]])
...work with n-dimensional arrays...
>>> foo = np.zeros((3,3,3))
>>> foo
array([[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]]])
>>> foo[0,...]
array([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
..do efficient linear algebra...
>>> foo = no.ones((3,3))
>>> np.linalg.qr(foo)
(array([[-0.57735027, 0.81649658, 0. ],
[-0.57735027, -0.40824829, -0.70710678],
[-0.57735027, -0.40824829, 0.70710678]]), array([[ -1.73205081e+00, -1.
73205081e+00, -1.73205081e+00],
[ 0.00000000e+00, -1.57009246e-16, -1.57009246e-16],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]]))
...and basically do anything that Matlab can.