Retrieving first value in list

694
5
10-20-2017 07:37 AM
MikeEdwards
New Contributor III

I'm trying to extract the first value from a list. I'm using [0] but it seems to be returning all the values regardless. Am I overlooking something?

env.workspace = r"someSDEconnection.sde"

addModDate_featureList = arcpy.ListFeatureClasses("GEO*")
for addModDate_feature in addModDate_featureList:
   fc_result = arcpy.GetCount_management(addModDate_feature)
   fc_count = int(fc_result.getOutput(0))
   if fc_count == 0:
      print(addModDate_feature + " No Geometry Present")
   else:
      addModDate_field_names = [f.name for f in arcpy.ListFields(addModDate_feature)]
      if "MOD_DATE" in addModDate_field_names:
      unique_addDate = set(row[0] for row in arcpy.da.SearchCursor(addModDate_feature, "MOD_DATE"))
      outList = []
      for element in unique_addDate:
         if element is None:
            pass
         elif element is not None and element not in outList:
            outList.append(element)
            outList.sort(reverse=True)
            outList_recent = outList[0]
            print(addModDate_feature + " " + outList_recent)

         else:
              print(addModDate_feature + " No Date")

Tags (1)
0 Kudos
5 Replies
MitchHolley1
MVP Regular Contributor

1.) Add brackets around your search cursor fields

2.) Remove the 'elif element is not None' from the elif statement since you're already checking if it's None in the above 'if' statement. 

3.) Use the GeoNet Syntax Highlighter. 

nv.workspace = r"someSDEconnection.sde"
addModDate_featureList = arcpy.ListFeatureClasses("GEO*")
for addModDate_feature in addModDate_featureList:
   fc_result = arcpy.GetCount_management(addModDate_feature)
   fc_count = int(fc_result.getOutput(0))
   if fc_count == 0:
      print(addModDate_feature + " No Geometry Present")
   else:
      addModDate_field_names = [f.name for f in arcpy.ListFields(addModDate_feature)]
      if "MOD_DATE" in addModDate_field_names:
      #add [] around the fields you're searching
      unique_addDate = set(row[0] for row in arcpy.da.SearchCursor(addModDate_feature, ["MOD_DATE"]))
      outList = []
      for element in unique_addDate:
        if element is None:
            pass
        elif element not in outList: #leave out checking if None, since that happens in 'if' above
            outList.append(element)
            outList.sort(reverse=True)
            outList_recent = outList[0]
            print(addModDate_feature + " " + outList_recent)
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If one is only passing a single field to a cursor, it doesn't have to be a list.  That said, passing a list regardless of the number of fields is a more consistent approach than a string for a single field and list for multiple fields.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I assume the bolded part is where you are having the issue.  At first glance, I am not sure why using the zero index is retrieving more than one value.  I ran a mock up using most of your code and didn't have an issue.  If you can put some print statements in and provide the community more feedback about values of variables along the way, we might be able to help more.

0 Kudos
MikeEdwards
New Contributor III
env.workspace = r"someSDEconnection.sde"

addModDate_featureList = arcpy.ListFeatureClasses("GEODBA*")
for addModDate_feature in addModDate_featureList:
    fc_result = arcpy.GetCount_management(addModDate_feature)
    fc_count = int(fc_result.getOutput(0))
    if fc_count == 0:
        print(addModDate_feature + " No Geometry Present")
    else:
        addModDate_field_names = [f.name for f in arcpy.ListFields(addModDate_feature)]
        if "MOD_DATE" in addModDate_field_names:
            unique_addDate = set(row[0] for row in arcpy.da.SearchCursor(addModDate_feature, ["MOD_DATE"]))
            outList = []
            for element in unique_addDate:
                if element is None:
                    pass
                elif element not in outList:
                   outList.append(element)
                   outList.sort(reverse=True)
                else:
                    pass
            if len(outList) == 0:
                pass
            else:
                outList_recent = outList[0]
                print(addModDate_feature + " " + str(outList_recent))

If I test for the Lists with no values it seems to work fine.

0 Kudos
RhettZufelt
MVP Frequent Contributor

This one I still use the old cursors as it is so simple.

myField = "recnum"
maxValue = arcpy.SearchCursor(infc, "", "", "", "recnum D").next().getValue(myField) #Get 1st row in descending cursor sort
‍‍‍‍‍

Only loading one field into the searchcursor, and in this case, it is numeric, so if I grab the first value, is the max, change the sort to "A" and would be the min.  In any case, it grabs the first value in the list.

R_

0 Kudos