I know that I can set up an integer before the loop and increment it together with the loop. But is there another way?
Asked
Active
Viewed 2,134 times
2 Answers
6
Just enumerate()
the iterable:
for index, obj in enumerate(objects):
print index
(The name object
is a built-in type. To avoid shadowing this name, I renamed the loop variable to obj
.)

Sven Marnach
- 574,206
- 118
- 941
- 841
2
You can use enumerate
for counter, obj in emumerate(objects):
print counter

Praveen Gollakota
- 37,112
- 11
- 62
- 61