[Pythonmac-SIG] repeat loop question
Bob Savage
bobsavage@mac.com
Thu, 07 Dec 2000 10:30:18 -0800
on 12/7/00 9:06 AM, tom smith wrote:
>
> n = 0
> the_list = ["a", "b", "c"]
>
> for i in the_list:
> print n, i
> n=n+1
>
>
> ...is there a way of getting the "count" of i without having to hand
> increment n?
>
Remember that in your example above i is not an index into the_list, it is
the actual item. You need to use something like this instead:
>>> theList = ["a", "b", "c"]
>>> for i in range(len(theList)):
... print i, theList[i]
...
0 a
1 b
2 c
Bob