G.add_node(1) # one node at a timeG.add_nodes_from([2, 3]) # from an interator# iterator of 2-tuples of the form (node, node_attributes)G.add_nodes_from([(4, { "color": "red" })], (5, { "color": "green" }))G.add_nodes_from(H) # import nodes from another graph, H
Removing
G.remove_node(2) # removes 2 from the graphG.remove_nodes_from("spam") # removes s, p, a, m from the graph
Accessing
G.add_node(1)G.nodes[1] # {} as it returns its attributes
Node attributes
# Add a node with an attributeG.add_node(1, time='5pm')# Add multiple nodes with the same attributeG.add_nodes_from([2,3], time='5pm')# Modify it laterG.nodes[2]['room'] = 714# Get node attributesG.nodes[1]# {'time': '5pm'}
Iterating through nodes
G.nodes.data() # returns a dict of node, node attribute pairs# { 1: { ...node attributes}, 2: { ...node attributes}}