Alex Punkallo
Alex Punkallo

Reputation: 119

TypeError: unhashable type: 'list' when i use a list of tuples to make a graph

This is a sample of a big list of tuples

import networkx as nx
import matplotlib.pyplot as plt

my_list=[(2496989087L, [114762303, 4046411357L, 3350679909L, 228860861]),(936533587, [1968901658, 2228506255L, 788861322, 3157824057L])]

I'd want to make a graph:

graph = nx.Graph()
graph.add_edges_from(my_list)

But I receive this error:

TypeError: unhashable type: 'list'

I don't understand the problem because the list is fine

Upvotes: 0

Views: 1918

Answers (1)

Eugene Yarmash
Eugene Yarmash

Reputation: 150031

The error indicates that an object of the list type was used as a dictionary key or a set element. In Python dictionary keys must be of immutable type (which list is not). To illustrate:

>>> D = {}
>>> D[(1, (2))] = 1
>>> D[(1, [2])] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> 

You're using incorrect data structure format for .add_edges_from(). From the docs:

The edges must be given as as 2-tuples (u,v) or 3-tuples (u,v,d) where d is a dictionary containing edge data.

Upvotes: 1

Related Questions