for statement on empty iterable

Amit Khemka khemkaamit at
Wed Aug 22 07:56:19 CEST 2007

On 8/22/07, james_027 <cai.haibin at> wrote:
> hi Paul,
> >
> > That doesn't crash or anything like that, but it also doesn't
> > set the index variable, which can cause confusion in some situations.
> Thanks for your quick answer ... Actually I was thinking how do I
> access the index inside a for statement? Can you help

Have a look at "enumerate", You can iterate over a list like:

for i,x in enumerate(['a', 'b', 'c']):
    print i, x

It prints:
0 a
1 b
2 c


Amit Khemka
Home Page:

Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.

More information about the Python-list mailing list