Skip to content
Advertisement

Removing self-loops from undirected networkx graph

I have created a graph from list of nodes using networkx. It has self loops. How to remove them? Following is sample:

JavaScript

I don’t want (1, 1) edges.

Advertisement

Answer

(instructions for networkx 1.x below)

If you’re using networkx 2.x try

JavaScript

If you have a MultiGraph (which for example configuration_model produces), this may not work if you have an older release of 2.x with a minor bug. If so and you don’t want to upgrade, then you need to convert this into a list before removing edges.

JavaScript

This bug has been corrected https://github.com/networkx/networkx/issues/4068.


In version 1.x (when I originally answered this question), it was:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement