0

I know that I can set up an integer before the loop and increment it together with the loop. But is there another way?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

2 Answers2

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