[Tutor] Re: when do I use this?

Lee Harr missive at hotmail.com
Mon Mar 15 19:40:26 EST 2004


>Several times I have iterated over a sequence with
>some code that looks like this:
>
>for f in range(len(files)):
>
>and fellow hackers have told me I can just use:
>
>for f in seq:
>


Almost always.

If you do not need to know the index numbers of the items in
the sequence, then just iterate over the sequence. So, to
extend your code ...

for n in range(len(files)):
    print 'processing file number', n+1
    file = files[n]
    file.process()


for file in files:
    file.process()

_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* 
http://join.msn.com/?page=features/junkmail




More information about the Tutor mailing list