import itertools def chunked(it, size): it = iter(it) while True: p = tuple(itertools.islice(it, size)) if not p: break yield p