Is enumerate in python lazy?

Viewed 43455

I'd like to know what happens when I pass the result of a generator function to python's enumerate(). Example:

def veryBigHello():
    i = 0
    while i < 10000000:
        i += 1
        yield "hello"

numbered = enumerate(veryBigHello())
for i, word in numbered:
    print i, word
    

Is the enumeration iterated lazily, or does it slurp everything into the <enumerate object> first? I'm 99.999% sure it's lazy, so can I treat it exactly the same as the generator function, or do I need to watch out for anything?

4 Answers

Old school alternative since I was using a generator that someone else (sklearn) wrote that didn't work with the approaches here.

i=(-1)
for x in some_generator:
    i+=1
Related