## Remove all edges from a graph in networkx

networkx copy graph

networkx prune graph

networkx edge attributes

networkx clear graph

networkx graph

networkx set edge weight

networkx print graph

I perform the following python commands, trying to remove all edges from the graph `G`

.

def method(G): edges = G.edges() G.remove_edges_from(edges)

But it yields the following error:

RuntimeError: dictionary changed size during iteration.

It seems like the command iterates through the edges, but when I remove an edge, it modifies the iterator during iteration, which causes an error.

How do I get around this?

You may want to look into the method `create_empty_copy`

, which copies the nodes without the edges into a new graph. The docs literally read:

Return a copy of the graph G with all of the edges removed.

If you wanted to keep the original graph as-is, you could do something like

edges = list(G.edges)

to ensure that you copy the edges before requesting their removal.

**networkx.Graph.clear,** Remove all nodes and edges from the graph. This also removes the name, and all graph, node, and edge attributes. Examples. >>> remove_edges_from¶. Remove all edges specified in ebunch. Each edge given in the list or container will be removed from the graph. The edges can be: 2-tuples (u,v) edge between u and v. 3-tuples (u,v,k) where k is ignored.

There are already couple methods provided by the `networkx`

package:

`remove_edges_from`

: remove all the edges from a graph`clear`

: remove all the nodes and edges from a graph

why simply not use any of them for your requirement? e.g.,

`G.remove_edges_from(G.edges())`

should do the job instead of defining yet another function to do the same. For example, consider the following code run on `python 3.5`

in windows 11 with `jupyter notebook`

:

import platform print(platform.python_version()) # 3.5.4 import networkx as nx print(nx.__version__) # 1.11 G=nx.complete_graph(5) print(G.edges()) # [(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] G.remove_edges_from(G.edges()) print(G.edges()) # []

**networkx.Graph.remove_edge,** NetworkXError – If there is not an edge between u and v. See also. remove_edges_from(). remove a collection of edges. Examples. > Creating a graph; Nodes; Edges; What to use as nodes and edges; Accessing edges; Adding attributes to graphs, nodes, and edges; Directed graphs; Multigraphs; Graph generators and graph operations; Analyzing graphs; Drawing graphs; Reference. Introduction; Graph types; Algorithms; Functions; Graph generators; Linear algebra; Converting to and

G.edges is an 'edges view' and G.edges() an 'edges data view' which are bound to the graph and are updated whenever the graph is altered.

You need to convert them into a self-contained independent variable which will not be updated each time an edge is removed from the graph, for instance by converting the edge view into a list or a dictionary.

Hence you can use these workarounds:

G.remove_edges_from(list(G.edges())) G.remove_edges_from(list(G.edges)) G.remove_edges_from(dict(G.edges))

**Remove all edges from a graph in networkx,** to ensure that you copy the edges before requesting their removal. There are already couple methods provided by the networkx package: remove_edges_from : remove all the edges from a graph. clear : remove all the nodes and edges from a graph. remove_edges_from¶. Remove all edges specified in ebunch. Each edge given in the list or container will be removed from the graph. The edges can be: 2-tuples (u,v) edge between u and v. 3-tuples (u,v,k) where k is ignored.

**networkx - Remove nodes and edges from a graph,** import networkx import matplotlib.pyplot as plt w = 4 h = 3 d = 70 plt.figure(figsize=(w, h), dpi=d) G = networkx.Graph() G.add_edges_from([(1, 2), (2, 3)]) How to DiGraph in networkx reserving weight of multiple edges and self-loops when removing all the multiple edges and self-loops 0 Remove reversible edges from directed graph networkx

**networkx.classes.graph,** If `None`, a NetworkX class (Graph or MultiGraph) is used. (n,)) for u in nbrs: del adj[u][n] # remove all edges n-u in graph del adj[n] # now remove node def If the graph is undirected, you can use . G.edges(node) In networkx 2.x this is an EdgeDataView object. In networkx 1.x this is a list - if you want a generator in 1.x rather than getting the whole list, G.edges_iter(node) works (this no longer exists in 2.x). If the graph is directed the command above will not give the in-edges. Use

**Graph – Undirected graphs with self loops,** Self loops are allowed but multiple (parallel) edges are not. to_networkx_graph() function, currently including edge list, dict of dicts, dict of lists, NetworkX graph, NumPy matrix or Graph.clear (), Remove all nodes and edges from the graph. remove_node¶ Graph.remove_node (n) [source] ¶ Remove node n. Removes the node n and all adjacent edges. Attempting to remove a non-existent node will raise an exception.

##### Comments

- What is
`type(edges)`

? Can you do`edges = G.edges().copy()`

? - Be careful that edges do not have the copy attribute! You should do something like:
`edges = list(G.edges.keys())`

- @Lucas. Thanks. Fixed.
- Your suggestion is effectively what the OP said failed.
`G.remove_edges_from(G.edges())`

doesn't work on my computer using version 2.0 (and gets the same error OP reports). OP is using`G.remove_edges_from(edges)`

where`edges=G.edges()`

, so I am reasonably confident it is equivalent to`G.remove_edges_from(G.edges())`

.- @Joel updated the
`os`

platform,`python`

version and`networkx`

version for which the above code works. - makes sense - in networkx 2.0 the output of
`G.edges()`

changed type. - I'm not sure how
`clear`

is relevant here, but it's good to know that the difference between the old and new version is that the new one presumably has less overhead.