I have 4,018 rows of data with about 50 fields. About 45 of those fields are the same variable, but the data came from different layers, so out of those 45 fields, only one has a value for the row, the rest are null. I just need the actual value in my final field. I used a string wildcard to identify the right fields. This code returns NULL for every row.

def n():
    import arcpy
    fields = arcpy.ListFields('Extract_fires')
    for f in fields:
        if f == 'AVGPRE*' and f is not None:
  • I'm using Calculate Field. I need to return the value with my function. So basically, my final field will eliminate null values and the merge all of those fields
    – Jared
    Dec 1, 2020 at 18:21
  • 'f.name == 'AVGPRE*' definitely makes sense, I'll try that out, thanks. The 'is not None' should refer to the field's value for that row. Hopefully that makes sense, sorry I'm new to python
    – Jared
    Dec 1, 2020 at 18:25
  • Ok nice. I would not use field calculator for this, to complicated. I would use da.UpdateCursor (which is "easier" when you get the hang of it)
    – BERA
    Dec 1, 2020 at 18:27
  • Okay, thanks, I'll try that out. I adjusted .ListFields to hold the wildcard, so it cut out that conditional in the if statement
    – Jared
    Dec 1, 2020 at 18:28
  • f.name == 'AVGPRE*' definitely doesn't make sense, because asterisk is not a permitted character in field names (alphanumeric and underscore only), so it will never evaluate True. You can test 'AVGPRE' in f.name.upper() to accomplish your goal. The None test is useless, since it will never be False.
    – Vince
    Dec 1, 2020 at 18:29

1 Answer 1


You can solve this using da.UpdateCursor:

import arcpy

fc = r'C:\folder\data.gdb\polygons123' #Change
field_to_hold_the_nonNonevalue = 'somefield' #Change
#List all fields starting with AVGPRE, and add the field to update last in list
fieldlist = [f.name for f in arcpy.ListFields(fc, 'AVGPRE*') if f.name != field_to_hold_the_nonNonevalue] + [field_to_hold_the_nonNonevalue]

with arcpy.da.UpdateCursor(fc, fieldlist) as cursor:
    for row in cursor: #For each row in the attribute table 
        #row is now a tuple of all fields values, for example (None, None, 1, None, ... None)
        value = [val for val in row[:-1] if val is not None][0] #Pick the first (index 0) non-None value in the row
        row[-1] = value #Set the last field in fieldlist to value
  • 1
    Awesome, thank you! I'm in a class right now, but I'll try it out right after.
    – Jared
    Dec 1, 2020 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.