I'm trying to build a dictionary from a table view in ArcMap using SearchCursor. Here is the snippet of code I'm having issues with:

valueDi = dict([(key, val) for (key, val) in
             (joinFc, [joinIdFld, joinValFld])])

This is the error I'm geting:

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 654, in run
    exec cmd in globals, locals
  File "N:\Common\Script\Drivers\PrepCleanScapesUpdate.py", line 1, in <module>
    import arcpy
ValueError: too many values to unpack

I've checked the brackets and parentheses against a snippet of code used in an answer to Using UpdateCursor for joined field in ArcPy?

  • the list within the searchcursor is a list not a dictionary so you cannot use the 2 iterators in looping through it – ziggy Oct 24 '16 at 21:50
  • I changed the second (key, val) to row and still getting too many values to unpack. valueDi = dict([(key, val) for row in arcpy.da.SearchCursor (joinFc, [joinIdFld, joinValFld])]) – geoJshaun Oct 24 '16 at 21:54
  • 3
    The first half of the code presented seems irrelevant to the error message that you are asking about. Do you still get the error when you import arcpy and then run only the code from joinFc = "GarbSeq" ? If so, then that should be your code snippet. – PolyGeo Oct 24 '16 at 22:17
  • Got rid of the first part. Thought working in Table View might have caused some issues. – geoJshaun Oct 25 '16 at 0:14
  • I think you got rid of too much because now you are using variables for which we can no longer see values, and you have not imported ArcPy. Perhaps review meta.gis.stackexchange.com/questions/4312/… – PolyGeo Oct 25 '16 at 1:06

I am not sure which field you want your dictionary key to be, but for the answer I am providing I made it the joinldFld, you can easy change it by switching the index.

valueDi = {x[0]: x[1] for x in arcpy.da.SearchCursor(joinFc, [joinIdFld, joinValFld])}

I originally thought you cannot use two iterators on the searchcursor because the fields are stored in a tuple or list but this worked for me as well. if someone can explain why the below worked it would be very beneficial to this problem

valueDi = {k:v for k,v in arcpy.da.SearchCursor(joinFc, [joinIdFld, joinValFld])}
  • NP. which one worked out for you? – ziggy Oct 25 '16 at 0:15
  • The first one {x[0]: x[1] for x in arcpy.da.SearchCursor(joinFc, [joinIdFld, joinValFld])} – geoJshaun Oct 25 '16 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.