I'm looking for a solution how to limit group size of a group created by itertools.groupby.
Currently I have something like this:
>>> s = '555'
>>> grouped = groupby(s)
>>> print([(k, len(list(g))) for k, g in grouped])
[('5', 3)]
What I would like achieve is to have a max group's size=2, so my output would be:
[('5', 2), ('5', 1)]
Is there any easy and efficient way to do this? Maybe somehow by key argument provided to groupby?