最も速く(そして最も理解しやすい)リスト内包表記を使用することです。
タイミングを参照してください。
import timeit
import random
c=10000
class SomeObj:
def __init__(self, i):
self.attr=i
def loopCR():
l=[]
for i in range(c):
l.append(SomeObj(random.random()))
return l
def compCR():
return [SomeObj(random.random()) for i in range(c)]
def loopAc():
lAttr=[]
for e in l:
lAttr.append(e.attr)
return lAttr
def compAc():
return [e.attr for e in l]
t1=timeit.Timer(loopCR).timeit(10)
t2=timeit.Timer(compCR).timeit(10)
print "loop create:", t1,"secs"
print "comprehension create:", t2,"secs"
print 'Faster of those is', 100.0*abs(t1-t2) / max(t1,t2), '% faster'
print
l=compCR()
t1=timeit.Timer(loopAc).timeit(10)
t2=timeit.Timer(compAc).timeit(10)
print "loop access:", t1,"secs"
print "comprehension access:", t2,"secs"
print 'Faster of those is', 100.0*abs(t1-t2) / max(t1,t2), '% faster'
プリント:
loop create: 0.103852987289 secs
comprehension create: 0.0848100185394 secs
Faster of those is 18.3364670069 % faster
loop access: 0.0206878185272 secs
comprehension access: 0.00913000106812 secs
Faster of those is 55.8677438315 % faster
したがって、リスト内包表記は、記述と実行の両方が高速です。