pythonpython-2.4enumerator

How to enumerate through list with the first "index" reported as 1? (Python 2.4)


I need my counter to start at 1. Right now I have

for(counter, file) in enumerate(files):
    counter += 1
    //do stuff with file and counter

But there must be a better way, in Python v2.4


Solution

  • Instead of counter += 1, maybe use counter + 1 where you've used counter.

    Alternatively:

    for counter, file in ((i + 1, f) for i, f in enumerate(files)):
        ...
    

    (Python 2.6 and later has some great stuff. Try to upgrade if you can.)