More

How to append Extent object to existing dictionary key values?

How to append Extent object to existing dictionary key values?


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.


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:UsersOWNERDocumentsArcGISCenterline_Edit.gdbCL_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:]] else: # if the relate key is already in the dictionary # append the next list of field values to the # existing list associated with the key relateDict[relateKey].append(relateRow[1:])