from itertools import groupby def pack_consecutive_duplicates(list1): return [list(group) for key, group in groupby(list1)]