EdgeView.data#

EdgeView.data(data=True, default=None, nbunch=None)#

Return a read-only view of edge data.

Parameters:
databool or edge attribute key

If data=True, then the data view maps each edge to a dictionary containing all of its attributes. If data is a key in the edge dictionary, then the data view maps each edge to its value for the keyed attribute. In this case, if the edge doesn’t have the attribute, the default value is returned.

defaultobject, default=None

The value used when an edge does not have a specific attribute

nbunchcontainer of nodes, optional (default=None)

Allows restriction to edges only involving certain nodes. All edges are considered by default.

Returns:
dataview

Returns an EdgeDataView for undirected Graphs, OutEdgeDataView for DiGraphs, MultiEdgeDataView for MultiGraphs and OutMultiEdgeDataView for MultiDiGraphs.

See also

EdgeDataView
OutEdgeDataView
MultiEdgeDataView
OutMultiEdgeDataView

Notes

If data=False, returns an EdgeView without any edge data.

Examples

>>> G = nx.Graph()
>>> G.add_edges_from(
...     [
...         (0, 1, {"dist": 3, "capacity": 20}),
...         (1, 2, {"dist": 4}),
...         (2, 0, {"dist": 5}),
...     ]
... )

Accessing edge data with data=True (the default) returns an edge data view object listing each edge with all of its attributes:

>>> G.edges.data()
EdgeDataView([(0, 1, {'dist': 3, 'capacity': 20}), (0, 2, {'dist': 5}), (1, 2, {'dist': 4})])

If data represents a key in the edge attribute dict, a dataview listing each edge with its value for that specific key is returned:

>>> G.edges.data("dist")
EdgeDataView([(0, 1, 3), (0, 2, 5), (1, 2, 4)])

nbunch can be used to limit the edges:

>>> G.edges.data("dist", nbunch=[0])
EdgeDataView([(0, 1, 3), (0, 2, 5)])

If a specific key is not found in an edge attribute dict, the value specified by default is used:

>>> G.edges.data("capacity")
EdgeDataView([(0, 1, 20), (0, 2, None), (1, 2, None)])

Note that there is no check that the data key is present in any of the edge attribute dictionaries:

>>> G.edges.data("speed")
EdgeDataView([(0, 1, None), (0, 2, None), (1, 2, None)])