SyntaxStudy
Sign Up
Python Intermediate 3 min read

itertools.islice

itertools.islice

itertools.islice() slices an iterator without materializing it — useful for taking the first N items of an infinite generator.

Example
from itertools import islice, count

first_ten = list(islice(count(1), 10))
print(first_ten)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Pro Tip

islice does not support negative indices like regular slicing.