Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

has_edge

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

Return True if the graph has an edge between nodes u and v.

Parameters:
  • v (u,) – Nodes can be, for example, strings or numbers.
  • key (hashable identifier, optional (default=None)) – If specified return True only if the edge with key is found.
Returns:

edge_ind – True if edge is in the graph, False otherwise.

Return type:

bool

Examples

Can be called either using two nodes u,v, an edge tuple (u,v), or an edge tuple (u,v,key).

>>> G = nx.MultiGraph()   # or MultiDiGraph
>>> G.add_path([0,1,2,3])
>>> G.has_edge(0,1)  # using two nodes
True
>>> e = (0,1)
>>> G.has_edge(*e)  #  e is a 2-tuple (u,v)
True
>>> G.add_edge(0,1,key='a')
>>> G.has_edge(0,1,key='a')  # specify key
True
>>> e=(0,1,'a')
>>> G.has_edge(*e) # e is a 3-tuple (u,v,'a')
True

The following syntax are equivalent:

>>> G.has_edge(0,1)
True
>>> 1 in G[0]  # though this gives KeyError if 0 not in G
True