Search code examples
pythonlistcursortuplesarcpy

SearchCursor in Arcpy Returning Tuples & Lists, Not a List


I have a fairly simple piece of code that searches a polygon feature class and stores the data for a selection of fields in a list:

for eachSMField in smFieldList:
    with arcpy.da.SearchCursor(seamaskPGN, eachSMField) as cursor:
        for row in cursor:
            cfbDataList.append(row)
        print("### cfbDataList: ")
        print(cfbDataList)

The last line of code above gives the following output:

[[(4.1,)], [(4.2,)], [(4.34,)], [(4.45,)], [(4.55,)], [(4.58,)], [(4.68,)], [(4.75,)], [(4.78,)], [(4.83,)], [(4.87,)], [(4.89,)], [(4.91,)], [(4.96,)], [(5.03,)], [(5.09,)]]

While the data is accurate, I cannot figure out why the data is 1) in a tuple and 2) each tuple is in their own list, in the wider list.

The output I'm looking for is simply the data in a list, e.g.:

[4.1, 4.2, 4.34, 4.45, ...etc]

Solution

  • The output of SearchCursor is an iterator of tuples. You're appending each row (tuple) to your list rather than the values themselves. Change your append statement to cfbDataList.append(row[0]) to append the value instead of the tuple.

    Another thing to check is the value of eachSMField you're passing into the cursor. It should be a list of fields...or, guessing at your intent, a list with one field name.