def _flatten(mapping):
    """Converts a hierarchical mapping to a flat dictionary"""
    result = {}
    for k, v in mapping.items():
        if isinstance(v, dict):
            for ik, iv in _flatten(v).items():
                ik = ik if isinstance(ik, tuple) else (ik,)
                result[(k,) + ik] = iv
        elif isinstance(v, list):
            for ik, iv in enumerate(_flatten(v)):
                ik = ik if isinstance(ik, tuple) else (ik,)
                result[(k,) + ik] = iv
        else:
            result[(k,)] = v
    return result