August 2018
Intermediate to advanced
366 pages
10h 14m
English
The itertools module is a treasure of valuable functions when working with iterables, and with minor effort it's possible to get the nth item of any iterable:
import itertools
def iter_nth(iterable, nth):
return next(itertools.islice(iterable, nth, nth+1))
Given a random iterable, we can use it to grab the element we want:
>>> values = (x for x in range(10)) >>> iter_nth(values, 4) 4