import itertools


def unsqueeze_list(listA, val=-1):
    """ Unpacks a list into a list of lists.

    Returns a list of lists by splitting the input list
    into 'N' lists when encounters an element equal to
    'val'. Empty lists resulting of trailing values at the
    end of the list are discarded.

    Source: https://stackoverflow.com/questions/4322705/split-a-list-into-nested-lists-on-a-value

    Parameters
    ----------
    listA : list
        A list.
    val : int/float, optional
        Value to separate the lists.

    Returns
    -------
    list
        A list of lists.

    Examples
    --------
    Unpack a list into a list of lists.

    >>> from dbcollection.utils.pad import unsqueeze_list
    >>> unsqueeze_list([1, 2, -1, 3, -1, 4, 5, 6], -1)
    [[1, 2], [3], [4, 5, 6]]

    """
    return [list(g) for k, g in itertools.groupby(listA, lambda x:x in (val, )) if not k]