a good explanation
sjmachin at lexicon.net
Thu May 25 14:40:19 CEST 2006
On 25/05/2006 9:27 PM, s99999999s2003 at yahoo.com wrote:
> my friend has written a loop like this
> cnt = 0
> files = [a,b,c,d]
> while cnt < len(files) :
> i told him using
> for fi in files:
> is better, because the while loop method makes another call to
> len..which is slower..
> am i partly right? or is there a better explanation for using the for
You are partially right.
(1) It's more elegant; you don't have the "cnt" if you don't need it,
there's no extraneous evaluation of len(files).
(2) You don't get the chance to omit cnt += 1, like your friend did
More information about the Python-list