MultiGraph.remove_nodes_from#
- MultiGraph.remove_nodes_from(nodes)#
Remove multiple nodes.
- Parameters:
- nodesiterable container
A container of nodes (list, dict, set, etc.). If a node in the container is not in the graph it is silently ignored.
See also
Notes
When removing nodes from an iterator over the graph you are changing, a
RuntimeError
will be raised with message:RuntimeError: dictionary changed size during iteration
. This happens when the graph’s underlying dictionary is modified during iteration. To avoid this error, evaluate the iterator into a separate object, e.g. by usinglist(iterator_of_nodes)
, and pass this object toG.remove_nodes_from
.Examples
>>> G = nx.path_graph(3) # or DiGraph, MultiGraph, MultiDiGraph, etc >>> e = list(G.nodes) >>> e [0, 1, 2] >>> G.remove_nodes_from(e) >>> list(G.nodes) []
Evaluate an iterator over a graph if using it to modify the same graph
>>> G = nx.Graph([(0, 1), (1, 2), (3, 4)]) >>> # this command will fail, as the graph's dict is modified during iteration >>> # G.remove_nodes_from(n for n in G.nodes if n < 2) >>> # this command will work, since the dictionary underlying graph is not modified >>> G.remove_nodes_from(list(n for n in G.nodes if n < 2))