Batches of Examples

Python for AIMedium+10 XP+10

Batches of Examples

Models are usually fed data a batch at a time rather than all at once.

Task: write a generator function batches(items, size) that hands out lists of size items, in order.

  • The last batch may be shorter if the items don't divide evenly.
  • Never hand out an empty batch: an empty items produces nothing at all.
  • size is at least 1.

Examples

list(batches([1, 2, 3, 4, 5], 2))
returns [[1, 2], [3, 4], [5]]
list(batches([], 3))
returns []

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run