jxn
jxn

Reputation: 8025

Python NetworkX adding list of edges with dict

I want to add a list of tuples of edges to my GRaph, and some of the attributes are dictionary. The documentation seem to be able to accept dictionary but i am getting an error:

G = nx.MultiDiGraph()

edges = [(34, 1, {'id': '123a'}, {'date': '2017-11-27'}),
 (1, 27, {'id': '123a'}, {'date': '2017-11-27'})]

G.add_edges_from(edges)

My error:

TypeError: unhashable type: 'dict'

Upvotes: 1

Views: 931

Answers (1)

rodgdor
rodgdor

Reputation: 2630

Try adding the edge attributes in a single dictionary:

G = nx.MultiDiGraph()

edges = [(34, 1, {'id': '123a', 'date': '2017-11-27'}),
 (1, 27, {'id': '123a', 'date': '2017-11-27'})]

G.add_edges_from(edges)
print(G[34][1][0]['id'])

ouput:

'123a'

Upvotes: 1

Related Questions