def map_over_dict_with_deletions(the_dict, fix_function):
    """Use this to map over dictionary. The fix function updates values, and returns
    None to delete a value.

    Returns the dictionary.

    Arguments
    ---------

    the_dict: A dictionary to fix.

    fix_function: A function that takes the dictionary key and value as
    arguments, and that returns None if the key is deleted, otherwise
    returns the updated value.

    """

    # this will update the dictionary and return a set of deleted ids
    deleted_keys = set()
    for key, val in list(the_dict.items()):
        updated_val = fix_function(key, val)
        if updated_val is None:
            # add to aggregation set
            deleted_keys.add(key)
            # delete the value
            del the_dict[key]
        else:
            # set the new value
            the_dict[key] = updated_val

    return the_dict