# How to create list from table

I have performed a spatial join on some data and now I would like to take that data and create a list. I'm trying to do this dynamically so that it can be easily repeatable via a script or model.

I have two fields in my data that I care about: Station Name and Page Number. The overall purpose of my project is to take a 632 page mapbook and divide it into smaller books based on the Station Name Boundary. Exporting to pdf only the pages that are relevant to each station.

My spatial join table has given me One_To_Many list of each page and what station boundary it falls within.

Now I'd like to create a list that is something like:

Station1
Pages: 1,2,3,4,5,6...

Station2
Pages: 8,9,10,11,12...


and so on.

For what its worth I'm using 9.3.1 and the maplogic extension for the mapbooks.

-
summarize the station name, then you can perform script or simply join it back to the spatjoin table –  Brad Nesom Feb 5 '13 at 14:30
Doing the summarize on the station name told me how many pages are within each boundary but not which pages those are. Feel like I'm forgetting something very basic here –  Craig Feb 5 '13 at 14:42
I think that you might need to expose c, but a simple export of columns in vbscript to excel. If you don't need scripts just copy and paste to excel and convert to a data file of your preference. –  lewis Feb 5 '13 at 14:47
I think my bad, try the summarize on the page number with a check on one of the options for the station. i.e. first. then follow @lewis and sort on station. –  Brad Nesom Feb 5 '13 at 16:28

I wound up creating a python script with significant help from the ESRI Python Forum. It utilizes SearchCursor and InsertCursor to scan through the table and place the results into a new table, joining them as described. The new table has to be created manually prior to running the script.

# Import the system modules
import os
import arcpy

# Set the parameters
table = r"path\to\source\table" # source_table
new_table = r"table.dbf" # new table name + extension only
tempTable = r"\\in_memory\temp_table"
outPath = r"path\to\output\location" # path to output location on disk
keyField = 'NAME'
valField = 'Pg_Number'

# Create a table dictionary of the keyfields outlined in the parameters
tableDict = {}
arcpy.MakeTableView_management(table, tempTable)
with arcpy.da.SearchCursor(table, [keyField, valField]) as cursor:
for row in cursor:
tableDict.setdefault(row[0], []).append(row[1])

# Export fields retrieved with SearchCursor into new table and join
insertCursor = arcpy.da.InsertCursor(os.path.join(outPath, new_table), [keyField, valField])
for key, val in tableDict.iteritems():
insertCursor.insertRow((key, ', '.join(str(item) for item in val)))

# Delete the tempTable
arcpy.Delete_management(r"\\in_memory\temp_table")
print 'The index has been created'

-