I'd argue against the temptation to treat generators like lists. The simple but naive approach is the simple one-liner:
gen = (i for i in range(10))
list(gen)[3]
But remember, generators aren't like lists. They don't store their intermediate results anywhere, so you can't go backwards. I'll demonstrate the problem with a simple example in the python repl:
>>> gen = (i for i in range(10))
>>> list(gen)[3]
3
>>> list(gen)[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Once you start going through a generator to get the nth value in the sequence, the generator is now in a different state, and attempting to get the nth value again will return you a different result, which is likely to result in a bug in your code.
Let's take a look at another example, based on the code from the question.
One would initially expect the following to print 4
twice.
gen = (i for i in range(10))
index = 4
for i, v in enumerate(gen):
if i == index:
answer = v
break
print(answer)
for i, v in enumerate(gen):
if i == index:
answer = v
break
print(answer)
but type this into the repl and you get:
>>> gen = (i for i in range(10))
>>> index = 4
>>> for i, v in enumerate(gen):
... if i == index:
... answer = v
... break
...
>>> print(answer)
4
>>> for i, v in enumerate(gen):
... if i == index:
... answer = v
... break
...
>>> print(answer)
9
Good luck tracing that bug down.
As pointed out, if the generator is infinitely long, you can't even convert it to a list. The expression list(gen)
will never finish.
There is a way you could put a lazily evaluated caching wrapper around an infinite generator to make it look like an infinitely long list you could index into at will, but that deserves its own question and answer, and would have major performance implications.
is
in this situation (or many situations at all).is
is for comparing identity, not equality. You want==
. This will probably work in this instance, but only by coincidence and implementation detail. – Widthindex
object to implement__eq__
in cases such as this? (This is getting off topic...) – Louvain1000 is 500 + 500
, it will (probably) beFalse
. See, for example, #306813 – Goghis
foris True
oris None
. Forint
using==
is fine, but not forfloat
where you should compare a difference to a tolerance like< 1e-7
. – Nymphalid