Warning

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

selfloop_edges

MultiGraph.selfloop_edges(data=False, keys=False, default=None)[source]

Return a list of selfloop edges.

A selfloop edge has the same node at both ends.

Parameters:
  • data (bool, optional (default=False)) – Return selfloop edges as two tuples (u,v) (data=False) or three-tuples (u,v,datadict) (data=True) or three-tuples (u,v,datavalue) (data=’attrname’)
  • default (value, optional (default=None)) – Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.
  • keys (bool, optional (default=False)) – If True, return edge keys with each edge.
Returns:

edgelist – A list of all selfloop edges.

Return type:

list of edge tuples

Examples

>>> G = nx.MultiGraph()   # or MultiDiGraph
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.selfloop_edges()
[(1, 1)]
>>> G.selfloop_edges(data=True)
[(1, 1, {})]
>>> G.selfloop_edges(keys=True)
[(1, 1, 0)]
>>> G.selfloop_edges(keys=True, data=True)
[(1, 1, 0, {})]