I'm working on this script which creates a table within a geodatabase. It then grabs the unique values from a list and attempts to insert them into the table using an Insert Cursor.

I get the Error

Runtime error
Traceback (most recent call last):
File "<string>", line 25, in <module>
TypeError: sequence size must match size of the row

My code is as follows:

import arcpy
from arcpy import env
env.workspace = "C:\Users\USER\Desktop\BaseLayer\UniqueValue.gdb"

## Designate the input layer.
featureclass = "Q:\Admin\IP_AnnoTools\Intermediates\Intersects\Output_Hydro_Line_Intersect.shp"## Parameter
field = "FEATURE_TY"## Parameter
valueList = []
rows = arcpy.SearchCursor(featureclass)
for row in rows:

uniqueSet = set(valueList)
uniqueList = list(uniqueSet)

arcpy.CreateTable_management("C:\Users\USER\Desktop\BaseLayer\UniqueValue.gdb", "unique")
unique = "C:\Users\USER\Desktop\BaseLayer\UniqueValue.gdb\unique"

cursor = arcpy.da.InsertCursor(unique,("UNIQUE_VAL"))

for uv in uniqueList:

del cursor
print uniqueList
del rows
del row

My output when I print my list is:


You have to insert a list, you're inserting a single value.

for uv in uniqueList:

should be

for uv in uniqueList:
| improve this answer | |

You are trying to put a string into the insertRow method, but that method expects a list or tuple. The insertrow method first counts the items in the list or tuple to see if the item count matches the row field count, but since a string item count is most likely 0 it always fails to match up with the field count of the row (i.e., 1). So instead try converting each string value into a list with a single string value. That way the count of items in the list will match the field count (1 string item in the list will match 1 string field in the row).

So modify your code to see if this works:

for uv in uniqueList:
| improve this answer | |

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.