MultiGraph.remove_edge#

MultiGraph.remove_edge(u, v, key=None)[source]#

Remove an edge between u and v.

Parameters:
u, vnodes

Remove an edge between nodes u and v.

keyhashable identifier, optional (default=None)

Used to distinguish multiple edges between a pair of nodes. If None, remove a single edge between u and v. If there are multiple edges, removes the last edge added in terms of insertion order.

Raises:
NetworkXError

If there is not an edge between u and v, or if there is no edge with the specified key.

See also

remove_edges_from

remove a collection of edges

Examples

>>> G = nx.MultiGraph()
>>> nx.add_path(G, [0, 1, 2, 3])
>>> G.remove_edge(0, 1)
>>> e = (1, 2)
>>> G.remove_edge(*e)  # unpacks e from an edge tuple

For multiple edges

>>> G = nx.MultiGraph()  # or MultiDiGraph, etc
>>> G.add_edges_from([(1, 2), (1, 2), (1, 2)])  # key_list returned
[0, 1, 2]

When key=None (the default), edges are removed in the opposite order that they were added:

>>> G.remove_edge(1, 2)
>>> G.edges(keys=True)
MultiEdgeView([(1, 2, 0), (1, 2, 1)])
>>> G.remove_edge(2, 1)  # edges are not directed
>>> G.edges(keys=True)
MultiEdgeView([(1, 2, 0)])

For edges with keys

>>> G = nx.MultiGraph()
>>> G.add_edge(1, 2, key="first")
'first'
>>> G.add_edge(1, 2, key="second")
'second'
>>> G.remove_edge(1, 2, key="first")
>>> G.edges(keys=True)
MultiEdgeView([(1, 2, 'second')])