itertools.islice()
itertools.islice() is a Python function in the Itertools Module category. Slices an iterator lazily (similar to sequence slicing but returns an iterator). The syntax is itertools.islice(iterable, stop) / islice(iterable, start, stop, step=1). Parameters: iterable, start, stop, step. Returns: Iterator. A typical example: list(islice(range(100), 5)) # [0,1,2,3,4]
list(islice(range(100), 10, 20, 3)) # [10,13,16,19]. A close sibling is itertools.chain(), which creates an iterator that returns elements from the first iterable until exhausted, then proceeds to the next. A close sibling is itertools.chain.from_iterable(), which alternative chain that takes a single iterable of iterables. A close sibling is itertools.product(), which cartesian product of input iterables (like nested for-loops). A close sibling is itertools.permutations(), which returns all possible r-length ordered arrangements of elements. More about this category: Iterator building blocks — chain, product, permutations, combinations, groupby. For a broader walkthrough, read the Itertools Module guide. Related Itertools Module entries: itertools.chain(), itertools.chain.from_iterable(), itertools.product(), itertools.permutations(), itertools.combinations(), itertools.combinations_with_replacement().