[Tutor] getting iteration level

2007-09-06 Thread David Bear
Lets say I have a list object that I iterate over like this:

for item in myList:
   process(item)

During execution of the for loop something happens and I want to know how
many items have be iterated over, how do I find out? Without resorting to
some counter inside the loop, is there some python object I can ask?


--
David Bear
College of Public Programs at Arizona State University

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] getting iteration level

2007-09-06 Thread Kent Johnson
David Bear wrote:
 Lets say I have a list object that I iterate over like this:
 
 for item in myList:
process(item)
 
 During execution of the for loop something happens and I want to know how
 many items have be iterated over, how do I find out? Without resorting to
 some counter inside the loop, is there some python object I can ask?

Use enumerate(), it generates the list index for you:

for i, item in enumerate(myList):
   process(item)
   print 'processed item', i

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor