import itertools
# chain — flatten iterables
list(itertools.chain([1, 2], [3, 4], [5])) # [1, 2, 3, 4, 5]
# groupby — group consecutive elements (input must be sorted)
data = sorted([{"env": "prod"}, {"env": "dev"}, {"env": "prod"}], key=lambda x: x["env"])
for key, group in itertools.groupby(data, key=lambda x: x["env"]):
print(key, list(group))
# islice — lazy slice of any iterable
list(itertools.islice(range(1000), 5, 10)) # [5, 6, 7, 8, 9]
# product — cartesian product
list(itertools.product([1, 2], ["a", "b"])) # [(1,'a'), (1,'b'), (2,'a'), (2,'b')]
# batched (Python 3.12+) — split iterable into chunks
list(itertools.batched(range(10), 3)) # [(0,1,2), (3,4,5), (6,7,8), (9,)]