[Tutor] list comprehension problem

Alan Gauld alan.gauld at btinternet.com
Sat Jul 4 01:17:43 CEST 2009


"Dinesh B Vadhia" <dineshbvadhia at hotmail.com> wrote

> As the lists of integers get larger ... the list comprehension 
> solution will get slower.  Do you agree?

Yes thats why Chris said the linear loop solution is almost 
certainly faster in this case. However you could speed up 
the for loop significantly by missing out the call to 
enumerate():

>>> for x in d:
>>>        y += x
>>>        dd.append(y)

Should give the same result...

List Comprehensions are not always the best answer.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list