Warning

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

edges

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

Return a list of edges.

Edges are returned as tuples with optional data and keys in the order (node, neighbor, key, data).

Parameters:
  • nbunch (iterable container, optional (default= all nodes)) – A container of nodes. The container will be iterated through once.
  • data (bool, optional (default=False)) – Return two tuples (u,v) (False) or three-tuples (u,v,data) (True).
  • keys (bool, optional (default=False)) – Return two tuples (u,v) (False) or three-tuples (u,v,key) (True).
Returns:

edge_list – Edges that are adjacent to any node in nbunch, or a list of all edges if nbunch is not specified.

Return type:

list of edge tuples

See also

edges_iter()
return an iterator over the edges

Notes

Nodes in nbunch that are not in the graph will be (quietly) ignored. For directed graphs this returns the out-edges.

Examples

>>> G = nx.MultiGraph()  # or MultiDiGraph
>>> G.add_path([0,1,2])
>>> G.add_edge(2,3,weight=5)
>>> G.edges()
[(0, 1), (1, 2), (2, 3)]
>>> G.edges(data=True) # default edge data is {} (empty dictionary)
[(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})]
>>> list(G.edges_iter(data='weight', default=1))
[(0, 1, 1), (1, 2, 1), (2, 3, 5)]
>>> G.edges(keys=True) # default keys are integers
[(0, 1, 0), (1, 2, 0), (2, 3, 0)]
>>> G.edges(data=True,keys=True) # default keys are integers
[(0, 1, 0, {}), (1, 2, 0, {}), (2, 3, 0, {'weight': 5})]
>>> list(G.edges(data='weight',default=1,keys=True))
[(0, 1, 0, 1), (1, 2, 0, 1), (2, 3, 0, 5)]
>>> G.edges([0,3])
[(0, 1), (3, 2)]
>>> G.edges(0)
[(0, 1)]