Re: [Tutor] when do I use this?

Magnus Lycka magnus at
Tue Mar 16 09:21:05 EST 2004

Christopher Spears wrote:
> 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:

Assuming files is a list of file objects, you would perhaps rather do:

for i in range(len(files)):
    f  = files[i]

(Using f for an index number confuses me at least.)

But if you don't need the index number for any particular purpose,
you are much better off skipping the detour via index numbers (which
you only do because other languages lack the ability to go straight.)
Then you get:

for f in files:

In recent versions of Python, you don't even need the range(len())
hack even if you need the index. Use the builtin function "enumerate"

for i, f in enumerate(files):
    print "Content of file number", i

Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65  mailto:magnus at

More information about the Tutor mailing list