from itertools import groupby data = [1, 1, 2, 2, 1, 1] print([(k, list(g)) for k, g in groupby(data)])