Split a List into Chunks
Medium · Lists
Slicing with items[start:stop] never fails when stop is past the end: it just returns what is there. And range(0, len(items), n) counts in steps of n.
Write chunk(items, size) that splits items into consecutive groups of size items. The last group may be shorter. An empty list gives []. size is always at least 1.