a good explanation
John Machin
sjmachin at lexicon.net
Thu May 25 08:40:19 EDT 2006
On 25/05/2006 9:27 PM, s99999999s2003 at yahoo.com wrote:
> hi
> my friend has written a loop like this
> cnt = 0
> files = [a,b,c,d]
> while cnt < len(files) :
> do_something(files[cnt])
>
> i told him using
> for fi in files:
> do_something(fi)
>
> 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
> method.?
You are partially right.
More reasons:
(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
(ROTFLMAO).
Cheers,
John
More information about the Python-list
mailing list