NodeView.data#
- NodeView.data(data=True, default=None)[source]#
Return a read-only view of node data.
- Parameters:
- databool or node data key, default=True
If
data=True(the default), return aNodeDataViewobject that maps each node to all of its attributes.datamay also be an arbitrary key, in which case theNodeDataViewmaps each node to the value for the keyed attribute. In this case, if a node does not have thedataattribute, thedefaultvalue is used.- defaultobject, default=None
The value used when a node does not have a specific attribute.
- Returns:
- NodeDataView
The layout of the returned NodeDataView depends on the value of the
dataparameter.
See also
Notes
If
data=False, returns aNodeViewobject without data.Examples
>>> G = nx.Graph() >>> G.add_nodes_from( ... [ ... (0, {"color": "red", "weight": 10}), ... (1, {"color": "blue"}), ... (2, {"color": "yellow", "weight": 2}), ... ] ... )
Accessing node data with
data=True(the default) returns a NodeDataView mapping each node to all of its attributes:>>> G.nodes.data() NodeDataView({0: {'color': 'red', 'weight': 10}, 1: {'color': 'blue'}, 2: {'color': 'yellow', 'weight': 2}})
If
datarepresents a key in the node attribute dict, a NodeDataView mapping the nodes to the value for that specific key is returned:>>> G.nodes.data("color") NodeDataView({0: 'red', 1: 'blue', 2: 'yellow'}, data='color')
If a specific key is not found in an attribute dict, the value specified by
defaultis returned:>>> G.nodes.data("weight", default=-999) NodeDataView({0: 10, 1: -999, 2: 2}, data='weight')
Note that there is no check that the
datakey is in any of the node attribute dictionaries:>>> G.nodes.data("height") NodeDataView({0: None, 1: None, 2: None}, data='height')