May 1, 2022
I came to the same idea. This can be fine-tuned with using sum(1 for x in group) instead of making a list each time.
UPDATE:
Quick check shows len(list()) is a bit faster. Hmmm.
But then using f-string is faster, than concatenation. I would never guessed without timeit.