def combine_dicts(dict1, dict2, which_values_are_key=1):
    """
    Suppose you have two dicts with a common key that you want to 'factor out'
    to form a new dict where one of the parent's values is the new key and the
    values are lists of the other dict's values. There is probably a concise
    name for this operation, but to be clear I'll give the concrete example
    from this application:

    Suppose you have been reading through a flat text file. Reading separate 
    lines at separate times, you discover a bunch of mappings from tag => score
    and tag => sequence. The tag is just an incidental common key though -- we
    care literally not at all about S_000001 for our purposes. What we really
    want and need is to be able to get 'all scores for a certain sequence.'
    """

    if which_values_are_key == 2:
        return combine_dicts(dict2, dict1, which_values_are_key=1)

    combined = {}
    for tag in dict2.keys():
        if dict1[tag] in combined:
            combined[dict1[tag]].append(dict2[tag])
        else:
            combined[dict1[tag]] = [dict2[tag]]
    return combined