Counting consecutive characters in a string python 2

from itertools import groupby
sm='aabccaaddbb'
print [[k, len(list(g))] for k, g in groupby(sm)]

output: [['a', 2], ['b', 1], ['c', 2], ['a', 2], ['d', 2], ['b', 2]]

Leave a comment