+ 1 for the lazy version. Why not using itertools.islice instead of the innermost for loop?