I want to use the field calculator to get the sum of a couple of columns. The problem is that some of them has Null Values. I am not able to find a solution. I tried this Python functions:
stack( !basic.Blue_Thickness!, !basic.Green_Thickness!, !basic.Red_Thickness!, !basic.F1_Thickness! )
Version 1:
def stack(*args):
myList = list(args)
myList = [item for item in myList if (item is not None)]
return sum(myList)
Version 2:
def stack(*args):
return sum(filter(None, args))
Version 3:
def stack(*args):
myList = list(args)
for item in myList:
if item is None:
myList.remove(item)
return sum(myList)
The result of all this functions is that I get always NULL back, only if there is a row with no NULL then I get a result.
Background: I have a File Geodatabase with one main table and some tables which joins this table. The calulation takes place only in the main table (source columns and write column). I am using ArcGIS 10.0. The Source of these functions is this discussion: Calculating field in which null values may be present?
if item
is the same asif (item is not None)
and easier to read. Likewiseif not item
is the same asif item == None
. I'm not sure if this has any bearing on your Q as I can't test in Arc10 or earlier. FWIW, v1 returns extra Nulls in Arc10.3 also, v3 appears to work properly but beware because mutating a list in place causes unexpected but not immediately obvious mistakes in the results (see here). Bottom line: v2 works, don't use the others.