list( myBigList[i] for i in [87, 342, 217, 998, 500] )
I compared the answers with python 2.5.2:
19.7 usec: [ myBigList[i] for i in [87, 342, 217, 998, 500] ]
20.6 usec: map(myBigList.__getitem__, (87, 342, 217, 998, 500))
22.7 usec: itemgetter(87, 342, 217, 998, 500)(myBigList)
24.6 usec: list( myBigList[i] for i in [87, 342, 217, 998, 500] )
Note that in Python 3, 1st was changed the same as on 4th.
Another option is to start with numpy.array , which allows indexing through a list or numpy.array :
>>> import numpy >>> myBigList = numpy.array(range(1000)) >>> myBigList[(87, 342, 217, 998, 500)] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: invalid index >>> myBigList[[87, 342, 217, 998, 500]] array([ 87, 342, 217, 998, 500]) >>> myBigList[numpy.array([87, 342, 217, 998, 500])] array([ 87, 342, 217, 998, 500])
tuple does not work like the ones that are slicers.
Dan D. Jul 09 2018-11-11T00: 00Z
source share