Python Forum

Full Version: ValueError: dictionary update sequence element #0 has length 1; 2
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
help resolve this error when trying to create network graph

# open nodes
with open('Twitter_SNA/nodes.csv', 'r')as nodescsv:
    nonreader = csv.reader(nodescsv)
    nodes2 = [n for n in nonreader][1:] # get list of node names
node_names = [n[0] for n in nodes2]
# open edges
with open('Twitter_SNA/edges.csv') as edgecsv:
    edgereader = csv.reader(edgecsv)
    edges3 = [tuple(e) for e in edgereader][1:] # retrieve list of edges

G.add_nodes_from(node_names)
G.add_edges_from(edges3)



---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-69-1da2e7f196f3> in <module>
      1 G.add_nodes_from(node_names)
----> 2 G.add_edges_from(edges3)

~\Anaconda3\lib\site-packages\networkx\classes\graph.py in add_edges_from(self, ebunch_to_add, **attr)
    976             datadict = self._adj[u].get(v, self.edge_attr_dict_factory())
    977             datadict.update(attr)
--> 978             datadict.update(dd)
    979             self._adj[u][v] = datadict
    980             self._adj[v][u] = datadict

ValueError: dictionary update sequence element #0 has length 1; 2 is required
The error is saying that G.add_edges_from is expecting to be passed an iterable that has each item being an iterable of 2.
edges3 = [tuple(e) for e in edgereader][1:] # retrieve list of edges
is passing in a list with each item being a tuple of 1 items, instead of a tuple of 2 items.
changing 1 to 2 still pops the following error

# open nodes
with open('Twitter_SNA/nodes.csv', 'r')as nodescsv:
    nonreader = csv.reader(nodescsv)
    nodes2 = [n for n in nonreader][1:] # get list of node names
node_names = [n[0] for n in nodes2]
# open edges
with open('Twitter_SNA/edges.csv') as edgecsv:
    edgereader = csv.reader(edgecsv)
    edges3 = [tuple(e) for e in edgereader][2:] # retrieve list of edges

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-84-1da2e7f196f3> in <module>
      1 G.add_nodes_from(node_names)
----> 2 G.add_edges_from(edges3)

~\Anaconda3\lib\site-packages\networkx\classes\graph.py in add_edges_from(self, ebunch_to_add, **attr)
    976             datadict = self._adj[u].get(v, self.edge_attr_dict_factory())
    977             datadict.update(attr)
--> 978             datadict.update(dd)
    979             self._adj[u][v] = datadict
    980             self._adj[v][u] = datadict

ValueError: dictionary update sequence element #0 has length 1; 2 is required
The problem is here tuple(e) a single item in the tuple, wants two items tuple(item1, item2)
thank you Yoris. i am wondering how to implement that would you help me