On 25/05/2006 9:27 PM, [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list