That whole `for` loop, up to and including this, could be smashing into a 2-liner with the help of `more_itertools.chunked`.