[Python-Dev] Adding start to enumerate()

Duncan Booth duncan.booth at suttoncourtenay.org.uk
Tue May 13 17:07:02 CEST 2008

"Steven D'Aprano" <steve at pearwood.info> wrote:

> On Mon, 12 May 2008 08:20:51 am Georg Brandl wrote:
>> I believe the following is a common use-case for enumerate()
>> (at least, I've used it quite some times):
>> for lineno, line in enumerate(fileobject):
>>      ...
>> For this, it would be nice to have a start parameter for enumerate().
> Why would it be nice? What would you use it for?
> The only thing I can think of is printing lines with line numbers, and 
> starting those line numbers at one instead of zero. If that's the only 
> use-case, should it require built-in support?
If you are generating paginated output then a function to generate an 
arbitrary page would likely want to enumerate starting at some value larger 
than one.

Of course in that case you'll also want to skip part way through the data, 
but I think it is more likely that you'll want to enumerate the partial 
data (e.g. if it is a database query) rather than slice the enumeration.

More information about the Python-Dev mailing list