English

Error: TypeError: sequence size must match size of the row

Error Message

When adding rows to a feature class or table using the InsertCursor ArcPy function, the following error is returned:

Error:   
TypeError: sequence size must match size of the row.

Cause

The cause of this error is usually either related to field matching or syntax errors.

  • Field matching error

    When setting up the values to be inserted into the feature class or table, the fields must match the defined fields within the cursor. This includes setting them in the correct order and ensuring that the field types and lengths match the data to be inserted.

    In the example below, the row values being inserted do not match up correctly with the InsertCursor fields.

    # fc is a feature class
    fc = "C:/data/texas.gdb/counties"
    
    rowValues = [('Anderson',(-84.0, 35.0)),
    	    ('Andrews', (-85.0, 36.0))]
    
    cur = arcpy.da.InsertCursor(fc,("NAME", "ID", "SHAPE@XY"))
    
    for row in rowValues:
        cur.insertRow(row)
    
  • Python syntax error

    There are many possible syntax errors, but this error usually relates to the way the data list is set up.

    In the example below, the XY coordinates are in a tuple that is not formatted correctly within the list to create the geometry.

    # fc is a feature class
    fc = "C:/data/texas.gdb/counties"
    
    rowValues = [((-84.0, 35.0)),
    	    ((-85.0, 36.0))]
    
    cur = arcpy.da.InsertCursor(fc,("SHAPE@XY"))
    
    for row in rowValues:
        cur.insertRow(row)
    

Solution or Workaround

Check to ensure that the row values being inserted line up with the InsertCursor fields, and the geometry lists are properly formatted.

Solution for the field matching error

Set only the necessary fields required for the data being inserted as shown in the example below:

# fc is a feature class
fc = "C:/data/texas.gdb/counties"

rowValues = [('Anderson',(-84.0, 35.0)),
	    ('Andrews', (-85.0, 36.0))]

cur = arcpy.da.InsertCursor(fc,("NAME", "SHAPE@XY"))

for row in rowValues:
    cur.insertRow(row)
Solution for the syntax error

When using the SHAPE@XY token to create a single point, a tuple with a single value is required. To be properly recognized, a single value tuple should be written with a trailing comma.

# fc is a feature class
fc = "C:/data/texas.gdb/counties"

rowValues = [((-84.0, 35.0),),
	    ((-85.0, 36.0),)]

cur = arcpy.da.InsertCursor(fc,("SHAPE@XY"))

for row in rowValues:
    cur.insertRow(row)

OR

Breaking out SHAPE@X and SHAPE@Y allows a tuple with a single value to be avoided.

# fc is a feature class
fc = "C:/data/texas.gdb/counties"

rowValues = [(-84.0, 35.0), (-85.0, 36.0))]

cur = arcpy.da.InsertCursor(fc,("SHAPE@X", "SHAPE@Y"))

for row in rowValues:
    cur.insertRow(row)

Related Information