Home Ask Login Register

Developers Planet

Your answer is one click away!

becko February 2016

How to get the edge between two nodes?

I want to get the edge between two nodes in a networkx graph. More specifically, I want to get some data associated with this edge. I know a priori that these two nodes are linked. Is there a function to do this?


Joel February 2016

The edge data are stored in a dictionary. To access that dictionary, use get_edge_data().

import networkx as nx
G.add_edge(1,2, weight=5)
> {'weight': 5}

If you want to iterate through all the edges you can use G.edges(data=True)

H = nx.Graph()
H.add_edge(2, 3, color = 'red')
H.add_edge(1, 2, weight = 4)
for u,v,data in H.edges_iter(data=True):
    print u, v, data
> 1 2 {'weight': 4}
> 2 3 {'color': 'red'}

Post Status

Asked in February 2016
Viewed 2,510 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life