from itertools import groupby
a = [1, 2, 3, 2, 2, 2, 3, 3, 4, 4, 4, 3, 1, 1]
a = sorted(a)
#Group contiguous elements
for key, group in groupby(a):
print list(group)
Execution result
[1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
[4, 4, 4]
Recommended Posts