[Tutor] changing list index start

Lie Ryan lie.1296 at gmail.com
Sat Sep 11 01:14:57 CEST 2010


On 09/11/10 07:36, Rance Hall wrote:
> I'm using the following function style I found on the net to create
> menus for a command line python script:
> 
<snip>
> It works well, but the first item is the list is item 0.  This is
> normal in most computing situations, but because this index is part of
> the output It would be nice if the first item in the list is item 1.

In most cases in Python, you would almost never need to reference the
list's index directly since python makes it easy to use iterators;
however in your particular case, which is a valid exception, enumerate()
takes an optional second argument `start` which defines the number that
enumerate start to count with, i.e. you can do:

for i, option in enumerate(mainmenuoptions, 1):
    print('%s. %s' % (i, option))

> php provided a way to change this, but I can find no documentation
> that says python can do this as well.



More information about the Tutor mailing list