Open this example in the interactive library
Source code
from itertools import groupby
print([(key, len(list(group))) for key, group in groupby('AAABBCC')])Sample output
[('A', 3), ('B', 2), ('C', 2)]
Group consecutive values. A self-contained example using the Python standard library.
Open this example in the interactive library
from itertools import groupby
print([(key, len(list(group))) for key, group in groupby('AAABBCC')])[('A', 3), ('B', 2), ('C', 2)]