for statement on empty iterable

Steve Holden steve at holdenweb.com
Wed Aug 22 07:07:23 EDT 2007


Amit Khemka wrote:
> 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
> 
Also remember that string are iterable:

 >>> for x in enumerate("James"):
...   print x
...
(0, 'J')
(1, 'a')
(2, 'm')
(3, 'e')
(4, 's')
 >>>

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list