set_node_attributes(G, values, name=None)[source]#

Sets node attributes from a given value or dictionary of values.


The call order of arguments values and name switched between v1.x & v2.x.

GNetworkX Graph
valuesscalar value, dict-like

What the node attribute should be set to. If values is not a dictionary, then it is treated as a single attribute value that is then applied to every node in G. This means that if you provide a mutable object, like a list, updates to that object will be reflected in the node attribute for every node. The attribute name will be name.

If values is a dict or a dict of dict, it should be keyed by node to either an attribute value or a dict of attribute key/value pairs used to update the node’s attributes.

namestring (optional, default=None)

Name of the node attribute to set if values is a scalar.


After computing some property of the nodes of a graph, you may want to assign a node attribute to store the value of that property for each node:

>>> G = nx.path_graph(3)
>>> bb = nx.betweenness_centrality(G)
>>> isinstance(bb, dict)
>>> nx.set_node_attributes(G, bb, "betweenness")
>>> G.nodes[1]["betweenness"]

If you provide a list as the second argument, updates to the list will be reflected in the node attribute for each node:

>>> G = nx.path_graph(3)
>>> labels = []
>>> nx.set_node_attributes(G, labels, "labels")
>>> labels.append("foo")
>>> G.nodes[0]["labels"]
>>> G.nodes[1]["labels"]
>>> G.nodes[2]["labels"]

If you provide a dictionary of dictionaries as the second argument, the outer dictionary is assumed to be keyed by node to an inner dictionary of node attributes for that node:

>>> G = nx.path_graph(3)
>>> attrs = {0: {"attr1": 20, "attr2": "nothing"}, 1: {"attr2": 3}}
>>> nx.set_node_attributes(G, attrs)
>>> G.nodes[0]["attr1"]
>>> G.nodes[0]["attr2"]
>>> G.nodes[1]["attr2"]
>>> G.nodes[2]

Note that if the dictionary contains nodes that are not in G, the values are silently ignored:

>>> G = nx.Graph()
>>> G.add_node(0)
>>> nx.set_node_attributes(G, {0: "red", 1: "blue"}, name="color")
>>> G.nodes[0]["color"]
>>> 1 in G.nodes