Oct-09-2023, 03:46 PM
(This post was last modified: Oct-11-2023, 11:54 AM by deanhystad.)
The first for loop uses up all the values in the iterable "e". There is nothing left to use. If you try to get another item, a StopIteration exception is raised.
e = enumerate("abcd") for index, letter in e: print(index, letter) next(e)
Output:0 a
1 b
2 c
3 d
Traceback (most recent call last):
File "...", line 4, in <module>
next(e)
StopIteration
A for loop does the next(iterable) and try/except StopIteration for you. Kind of like this:e = enumerate("abcd") while True: try: index, letter = next(e) print(index, letter) except StopIteration: break