iterTools.py 390 B

123456789101112
  1. from itertools import *
  2. # Python 3.12:
  3. if "batched" not in globals():
  4. # https://docs.python.org/3/library/itertools.html#itertools.batched
  5. def batched(iterable, n):
  6. # batched('ABCDEFG', 3) --> ABC DEF G
  7. if n < 1:
  8. raise ValueError("n must be at least one")
  9. it = iter(iterable)
  10. while batch := tuple(islice(it, n)):
  11. yield batch