Python Forum
ValueError: dictionary update sequence element #0 has length 1; 2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: dictionary update sequence element #0 has length 1; 2
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
The problem is here tuple(e) a single item in the tuple, wants two items tuple(item1, item2)
Reply
#5
thank you Yoris. i am wondering how to implement that would you help me
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  which exception should be thrown for a sequence of the wrong length? Skaperen 1 849 Jan-06-2023, 04:13 AM
Last Post: deanhystad
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,135 May-17-2022, 11:38 AM
Last Post: Larz60+
  check if element is in a list in a dictionary value ambrozote 4 1,967 May-11-2022, 06:05 PM
Last Post: deanhystad
  Why is dictionary length not 20? Mark17 2 1,737 Oct-07-2021, 05:56 PM
Last Post: Mark17
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,684 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  removing dictionary element in list using (key, value) MelonMusk 3 2,296 Jun-13-2020, 02:37 PM
Last Post: buran
  Highlight/Underline a string | ValueError: zero length field name in format searching1 1 3,023 Jul-01-2019, 03:06 AM
Last Post: metulburr
  update values in list based on dictionary bunti 3 2,834 Jun-10-2019, 07:26 AM
Last Post: perfringo
  compare element of a dictionary Amniote 1 2,162 May-23-2019, 05:21 PM
Last Post: woooee
  Unable to locate element no such element gahhon 6 4,475 Feb-18-2019, 02:09 PM
Last Post: gahhon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020