def _invert_and_aggregate(the_dict):
    """
    Transform a dictionary like this:

    { key1: value1,
      key2: value2,
      key3: value2 }

    Into a dictionary of lists like this:

    { value1: [ key1 ],
      value2: [ key2, key3 ] }

    See tests.py for an example:
    """
    _ = {}
    # invert it
    for k, v in the_dict.items():
        _.setdefault(v, []).append(k)
    return _