Reputation: 163
if we have the following code:
s = "stackoverflow"
si = iter(s)
si.__next__() # Would return s
si.__next__() # Would return t
si.__next__() # Would return a
is there a way without using loops I can get "a"
with a single call to __next__()
. so basically I want to call __next__()
three times and get the third value. (Note that an iterator must be used,I can't just use slicing)
Thank you
Upvotes: 5
Views: 2551
Reputation: 250881
You can use itertools.islice
:
>>> from itertools import islice
>>> si = iter(s)
>>> next(islice(si, 2, 3))
'a'
Upvotes: 10