Reverses the keys and values in a dictionary. Set unique_vals to False if
    the values in the dict are not unique.

    Args:
        dict_ (dict_): dictionary
        unique_vals (bool): if False, inverted keys are returned in a list.

    Returns:
        dict: inverted_dict

    CommandLine:
        python -m utool.util_dict --test-invert_dict

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_dict import *  # NOQA
        >>> import utool as ut
        >>> dict_ = {'a': 1, 'b': 2}
        >>> inverted_dict = invert_dict(dict_)
        >>> result = ut.repr4(inverted_dict, nl=False)
        >>> print(result)
        {1: 'a', 2: 'b'}

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_dict import *  # NOQA
        >>> import utool as ut
        >>> dict_ = OrderedDict([(2, 'good',), (1, 'ok',), (0, 'junk',), (None, 'UNKNOWN',)])
        >>> inverted_dict = invert_dict(dict_)
        >>> result = ut.repr4(inverted_dict, nl=False)
        >>> print(result)
        {'good': 2, 'ok': 1, 'junk': 0, 'UNKNOWN': None}

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_dict import *  # NOQA
        >>> import utool as ut
        >>> dict_ = {'a': 1, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 2}
        >>> inverted_dict = invert_dict(dict_, unique_vals=False)
        >>> inverted_dict = ut.map_dict_vals(sorted, inverted_dict)
        >>> result = ut.repr4(inverted_dict, nl=False)
        >>> print(result)
        {0: ['b', 'c', 'd', 'e'], 1: ['a'], 2: ['f']}

def invert_dict(dict_, unique_vals=True):
    """
    Reverses the keys and values in a dictionary. Set unique_vals to False if
    the values in the dict are not unique.

    Args:
        dict_ (dict_): dictionary
        unique_vals (bool): if False, inverted keys are returned in a list.

    Returns:
        dict: inverted_dict

    CommandLine:
        python -m utool.util_dict --test-invert_dict

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_dict import *  # NOQA
        >>> import utool as ut
        >>> dict_ = {'a': 1, 'b': 2}
        >>> inverted_dict = invert_dict(dict_)
        >>> result = ut.repr4(inverted_dict, nl=False)
        >>> print(result)
        {1: 'a', 2: 'b'}

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_dict import *  # NOQA
        >>> import utool as ut
        >>> dict_ = OrderedDict([(2, 'good',), (1, 'ok',), (0, 'junk',), (None, 'UNKNOWN',)])
        >>> inverted_dict = invert_dict(dict_)
        >>> result = ut.repr4(inverted_dict, nl=False)
        >>> print(result)
        {'good': 2, 'ok': 1, 'junk': 0, 'UNKNOWN': None}

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_dict import *  # NOQA
        >>> import utool as ut
        >>> dict_ = {'a': 1, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 2}
        >>> inverted_dict = invert_dict(dict_, unique_vals=False)
        >>> inverted_dict = ut.map_dict_vals(sorted, inverted_dict)
        >>> result = ut.repr4(inverted_dict, nl=False)
        >>> print(result)
        {0: ['b', 'c', 'd', 'e'], 1: ['a'], 2: ['f']}
    """
    if unique_vals:
        inverted_items = [(val, key) for key, val in six.iteritems(dict_)]
        inverted_dict = type(dict_)(inverted_items)
    else:
        inverted_dict = group_items(dict_.keys(), dict_.values())
    return inverted_dict