util.py 229 B

123456789101112
  1. def batches(iterable, n):
  2. if not isinstance(iterable, list):
  3. iterable = list(iterable)
  4. if not iterable:
  5. return []
  6. i = 0
  7. while i < len(iterable):
  8. yield iterable[i:i+n]
  9. i += n