Get loop count inside a Python FOR loop Get loop count inside a Python FOR loop python python

Get loop count inside a Python FOR loop


The pythonic way is to use enumerate:

for idx,item in enumerate(list):


Agree with Nick. Here is more elaborated code.

#count=0for idx, item in enumerate(list):    print item    #count +=1    #if count % 10 == 0:    if (idx+1) % 10 == 0:        print 'did ten'

I have commented out the count variable in your code.


I know rather old question but....came across looking other thing so I give my shot:

[each*2 for each in [1,2,3,4,5] if each % 10 == 0])