for statement on empty iterable

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


On 8/22/07, james_027 <cai.haibin at gmail.com> 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

cheers,
amit

----
Amit Khemka
website: www.onyomo.com
wap-site: www.owap.in
Home Page: www.cse.iitd.ernet.in/~csd00377

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