Warning

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

networkx.classes.function.selfloop_edges

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

Returns an iterator over selfloop edges.

A selfloop edge has the same node at both ends.

Parameters:
  • data (string or 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’)
  • keys (bool, optional (default=False)) – If True, return edge keys with each edge.
  • default (value, optional (default=None)) – Value used for edges that dont have the requested attribute. Only relevant if data is not True or False.
Returns:

edgeiter – An iterator over all selfloop edges.

Return type:

iterator over edge tuples

Examples

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