def invert_dict(d):
    """
    Invert a dictionary by mapping values to keys.
    If the val:key pair exists, then val maps to a list of keys.
    Return a new dictionary containing val:key pairs.
    """
    inverse = dict()
    for key in d:
        val = d[key]
        inverse.setdefault(val, []).append(key)
#        if val in inverse:
#            inverse[val].append(key)
#        else:
#            inverse[val] = [key]
    return inverse