Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change a class of edges to a list in NetworkX?

I tried to create a list from the class of edges in NetworkX using the function list(). However, when I access the list using print(data[0]), I get the edge and all its attributes but I only want the edge. How can I access the edge alone?

edges = g.edges()
print("Type = " ,type(edges))
      
print("All edges: ", edges )

#Changing the class of edges into a list
data = list(edges. items())

print("First edge = ", data[0])

This is the output of the code:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You don’t need to use items:

edges = list(g.edges())
print(edges)

# Output
[(0, 1),
 (0, 2),
 (0, 3),
 (0, 4),
 (0, 5),
 (0, 6),
 (0, 7),
 (0, 8),
 (0, 10),
 ...]

items return the edges and the associated data.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading