There's nothing wrong with your original implementation but if you insist oneliner here's one ugly option:
from itertools import accumulate, chain, groupby
THRESHOLD = 3
l = [[1], [1, 1], [1, 1, 1], [1], [1], [1], [1, 1, 1, 1]]
res = [[y for x in g for y in x[1]]
for k, g in groupby(zip(chain([0], accumulate(len(x) for x in l)), l),
lambda x: x[0] // THRESHOLD)]
print(res)
Output:
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1, 1]]
The idea is to generate list of (item count so far, sublist)
tuples and group them by dividing count by THRESHOLD
.
>>> temp = list(zip(chain([0], accumulate(len(x) for x in l)), l))
>>> temp
[(0, [1]), (1, [1, 1]), (3, [1, 1, 1]), (6, [1]), (7, [1]), (8, [1]), (9, [1, 1, 1, 1])]
>>> groups = [list(g) for k, g in groupby(temp, lambda x: x[0] // THRESHOLD)]
>>> groups
[[(0, [1]), (1, [1, 1])], [(3, [1, 1, 1])], [(6, [1]), (7, [1]), (8, [1])], [(9, [1, 1, 1, 1])]]
>>> [[y for x in g for y in x[1]] for g in groups]
[[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1, 1]]