Remove node n. Removes the node n and all adjacent edges. Attempting to remove a nonexistent node will raise an exception. Parameters ---------- n : node A node in the graph Raises ------ NetworkXError If n is n
(self, n)
| 660 | nx._clear_cache(self) |
| 661 | |
| 662 | def remove_node(self, n): |
| 663 | """Remove node n. |
| 664 | |
| 665 | Removes the node n and all adjacent edges. |
| 666 | Attempting to remove a nonexistent node will raise an exception. |
| 667 | |
| 668 | Parameters |
| 669 | ---------- |
| 670 | n : node |
| 671 | A node in the graph |
| 672 | |
| 673 | Raises |
| 674 | ------ |
| 675 | NetworkXError |
| 676 | If n is not in the graph. |
| 677 | |
| 678 | See Also |
| 679 | -------- |
| 680 | remove_nodes_from |
| 681 | |
| 682 | Examples |
| 683 | -------- |
| 684 | >>> G = nx.path_graph(3) # or DiGraph, MultiGraph, MultiDiGraph, etc |
| 685 | >>> list(G.edges) |
| 686 | [(0, 1), (1, 2)] |
| 687 | >>> G.remove_node(1) |
| 688 | >>> list(G.edges) |
| 689 | [] |
| 690 | |
| 691 | """ |
| 692 | adj = self._adj |
| 693 | try: |
| 694 | nbrs = list(adj[n]) # list handles self-loops (allows mutation) |
| 695 | del self._node[n] |
| 696 | except KeyError as err: # NetworkXError if n not in self |
| 697 | raise NetworkXError(f"The node {n} is not in the graph.") from err |
| 698 | for u in nbrs: |
| 699 | del adj[u][n] # remove all edges n-u in graph |
| 700 | del adj[n] # now remove node |
| 701 | nx._clear_cache(self) |
| 702 | |
| 703 | def remove_nodes_from(self, nodes): |
| 704 | """Remove multiple nodes. |