I have a dictionary whose keys are map segment names, and whose value is a list of three field values.

Using an arcpy.da.SearchCursor on the map segments FeatureLayer, I have extracted the extent object and the names for the segments.

Using the similar map segment names, I want to append the extent values to the dictionary keys (so that I can reference them later). How would I do this, you can't index an Extent object.

for instance:

{'m32': [7, 10, 13], 'm33': [7, 10, 13]}...

turns into something like:

{'m32': [7, 10, 13,XMin,Ymin,XMax,YMax], 'm33': [7, 10, 13,XMin,Ymin,XMax,YMax]}...

so that when I index it, I can index the key, the original values, and index the extent values.

  • You can add extent to values in your dictionary. Alternatively use ext.XMin, ext.YMin etc .
    – FelixIP
    Dec 10 '15 at 22:37
  • 2
    adding values to a dictionary is a pure Python question
    – gene
    Dec 11 '15 at 10:40

For adding a new items to a python list object use append. The basic syntax is list.append(addedItem). With a dictionary containing a list it is dictionary(key).append(addedItem).

Here is a script skeleton that creates a dictionary which has a list of lists as its value:

 # Set feature class variable
 relateFC = r"C:\Users\OWNER\Documents\ArcGIS\Centerline_Edit.gdb\CL_INTERSECTIONS_PAIRS"  
    # create a field list with the relate field first (ROUTE_NAME),   
    # followed by sort field(s) (MEASURE), then label field(s) (CROSS_STREET)  
    relateFieldsList = ["ROUTE_NAME", "MEASURE", "CROSS_STREET"]  
    # process a da search cursor to transfer the data to the dictionary  
    with arcpy.da.SearchCursor(relateFC, relateFieldsList) as relateRows:  
      for relateRow in relateRows:  
        # store the key value in a variable so the relate value   
        # is only read from the row once, improving speed  
        relateKey = relateRow[0]  
        # if the relate key of the current row isn't found   
        # create the key and make it's value a list of a list of field values  
        if not relateKey in relateDict:  
          # [searchRow[1:]] is a list containing  
          # a list of the field values after the key.  
          relateDict[relateKey] = [relateRow[1:]]  
          # if the relate key is already in the dictionary   
          # append the next list of field values to the   
          # existing list associated with the key  

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.