def map_attr(attr, keys, values, fg):
    """Map the values to the `attr` attribute of elements in fg.

    If keys are singletons, assumes that they are nodes. In all other cases,
    assumes they are edges.
    """
    assert len(keys) == len(values)
    if len(tuple(keys[0])) == 1:
        for node, value in zip(keys, values):
            fg.nodes[node][attr] = value
    else:
        for edge, value in zip(keys, values):
            fg.edges[edge][attr] = value
