Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Module itertools

Compose iterators for lazy transformations, grouping and combinatorics.

Version: Python 3.14; repository image, production runtime not attested

Syntax

itertools.islice(iterator, stop)

Compose iterators for lazy transformations, grouping and combinatorics.

An infinite count can be consumed safely by bounding it with islice. The iterators returned by groupby share their source and group consecutive matching keys only.

A key that appears again after a different key starts a new group.

Examples

import itertools
series = itertools.count(3, 2)
print(list(itertools.islice(series, 3)), next(series))
groups = [(k, len(list(g))) for k, g in itertools.groupby("AABA")]
print(groups)

Result

[3, 5, 7] 9
[('A', 2), ('B', 1), ('A', 1)]

A key that appears again after a different key starts a new group.

Sources