def invert_index(idx):
    """Returns a new index with the same values, but structured differently in that
    in the output top-level keys switch place with embedded keys. For example,
    "k1 -> k2 -> value" will be turned into "k2 -> k1 -> value". The values
    themselves do not change."""
    result = {}
    for fname in idx:
        for lemma in idx[fname]:
            result.setdefault(lemma, {})
            result[lemma][fname] = idx[fname][lemma]
    return result