Search code examples
pythonlistclassnetworkxedges

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


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.