Lazy expressions and islice
Learn in depth →A generator expression computes values as requested; islice limits consumption.
(expression for item in iterable)
itertools.islice(iterator, count)Example
from itertools import islice
g = (n * n for n in range(100))
print(list(islice(g, 3)))
print(next(g))Result
[0, 1, 4] 9
Watch for: Laziness does not make retained source data disappear. Some operations buffer or consume the entire input.
Practise this topic →