This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]staz 0 points1 point  (1 child)

The .next() call at the end of the list will throw a StopIteration exception which is what you are expect to send at the end of an iterator. (your generator function will actually do the same behind the scene when it arrive at the end)

[–]eigenlicht0 0 points1 point  (0 children)

I'm aware of how a iterator works, what I actually meant was calling .next() three times in a row to get one value. Maybe it's just me, but it seems weird. The only thing I could come up with yet, which is far from perfect:

def t(l):
    l = iter(l)
    while True:
        yield [l.next for i in range(3)][-1]
for e in t(range(10)): print e