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.


A NetworkX graph.

datastring 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’)

keysbool, optional (default=False)

If True, return edge keys with each edge.

defaultvalue, optional (default=None)

Value used for edges that don’t have the requested attribute. Only relevant if data is not True or False.

edgeiteriterator over edge tuples

An iterator over all selfloop edges.


>>> 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, {})]