def rename_vertex(n, keep, left=True):
    """
    Rename a vertex: the vertices from the list ``keep`` get
    relabeled 0, 1, 2, ..., in order.  Any other vertex (e.g. 4) gets
    renamed to by prepending an 'L' or an 'R' (thus to either 'L4' or
    'R4'), depending on whether the argument left is ``True`` or ``False``.

    :param n: a 'vertex': either an integer or a string
    :param keep: a list of three vertices
    :param left: if ``True``, rename for use in left factor
    :type left: boolean; optional, default ``True``

    This is used by the :meth:`~SimplicialComplex.connected_sum` method for
    simplicial complexes.

    EXAMPLES::

        sage: from sage.homology.simplicial_complex import rename_vertex
        sage: rename_vertex(6, [5, 6, 7])
        1
        sage: rename_vertex(3, [5, 6, 7, 8, 9])
        'L3'
        sage: rename_vertex(3, [5, 6, 7], left=False)
        'R3'
    """
    lookup = {i:v for v,i in enumerate(keep)}
    try:
        return lookup[n]
    except KeyError:
        if left:
            return "L" + str(n)
        else:
            return "R" + str(n)