Ungroups items

    Args:
        grouped_items (list):
        groupxs (list):
        maxval (int): (default = None)

    Returns:
        list: ungrouped_items

    SeeAlso:
        vt.invert_apply_grouping

    CommandLine:
        python -m utool.util_alg ungroup_unique

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_alg import *  # NOQA
        >>> import utool as ut
        >>> grouped_items = [[1.1, 1.2], [2.1, 2.2], [3.1, 3.2]]
        >>> groupxs = [[0, 2], [1, 5], [4, 3]]
        >>> maxval = None
        >>> ungrouped_items = ungroup(grouped_items, groupxs, maxval)
        >>> result = ('ungrouped_items = %s' % (ut.repr2(ungrouped_items),))
        >>> print(result)
        ungrouped_items = [1.1, 2.1, 1.2, 3.2, 3.1, 2.2]

def ungroup(grouped_items, groupxs, maxval=None, fill=None):
    """
    Ungroups items

    Args:
        grouped_items (list):
        groupxs (list):
        maxval (int): (default = None)

    Returns:
        list: ungrouped_items

    SeeAlso:
        vt.invert_apply_grouping

    CommandLine:
        python -m utool.util_alg ungroup_unique

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_alg import *  # NOQA
        >>> import utool as ut
        >>> grouped_items = [[1.1, 1.2], [2.1, 2.2], [3.1, 3.2]]
        >>> groupxs = [[0, 2], [1, 5], [4, 3]]
        >>> maxval = None
        >>> ungrouped_items = ungroup(grouped_items, groupxs, maxval)
        >>> result = ('ungrouped_items = %s' % (ut.repr2(ungrouped_items),))
        >>> print(result)
        ungrouped_items = [1.1, 2.1, 1.2, 3.2, 3.1, 2.2]
    """
    if maxval is None:
        # Determine the number of items if unknown
        maxpergroup = [max(xs) if len(xs) else 0 for xs in groupxs]
        maxval = max(maxpergroup) if len(maxpergroup) else 0
    # Allocate an array containing the newly flattened items
    ungrouped_items = [fill] * (maxval + 1)
    # Populate the array
    for itemgroup, xs in zip(grouped_items, groupxs):
        for item, x in zip(itemgroup, xs):
            ungrouped_items[x] = item
    return ungrouped_items