Skip to main content
added 127 characters in body
Source Link
MrPugh
  • 43
  • 1
  • 1
  • 5

I want to step the loop iterator explicitly inside the loop. Is there a 'nicer' way to do this than:

idx = iter(range(0, 10))

for i in idx:
   print i

   if i == 5:
      print "consuming %i in step %i" % (next(idx), i)

Edit: I wander if there is a way to get access to the loop-iterator other than defining it explicitly as in my example.

Thanks!

I want to step the loop iterator explicitly inside the loop. Is there a 'nicer' way to do this than:

idx = iter(range(0, 10))

for i in idx:
   print i

   if i == 5:
      print "consuming %i in step %i" % (next(idx), i)

Thanks!

I want to step the loop iterator explicitly inside the loop. Is there a 'nicer' way to do this than:

idx = iter(range(0, 10))

for i in idx:
   print i

   if i == 5:
      print "consuming %i in step %i" % (next(idx), i)

Edit: I wander if there is a way to get access to the loop-iterator other than defining it explicitly as in my example.

Thanks!

Source Link
MrPugh
  • 43
  • 1
  • 1
  • 5

Python: access to iterator-object in for-loops

I want to step the loop iterator explicitly inside the loop. Is there a 'nicer' way to do this than:

idx = iter(range(0, 10))

for i in idx:
   print i

   if i == 5:
      print "consuming %i in step %i" % (next(idx), i)

Thanks!