Pack consecutive duplicates of list elements into sublists python code example

Example: Pack consecutive duplicates of list elements into sublists python

import itertools

def pack_consecutive_duplicates(_list: list):
    preprocessed_list = []
    # x groups the duplicates into a tuple ()
    # x[0] is the duplicate element
    # x[1] is a grouper iterable that conatins all the duplicate elements
    for x in itertools.groupby(_list):
        amount = [*x[1]].count(x[0])
        if amount > 1:
            for k in range(0, amount):
                preprocessed_list.append(x[0])
        else:
            preprocessed_list.append((x[0]))

    return preprocessed_list