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: 
  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?

  • Have you tried 'item == None'? – PolyGeo Jun 30 '13 at 4:53
  • thank you unfortunately it didn't worked. i tried it two times: 'code' def stack(*args): myList = list(args) for item in myList: if item == None: myList.remove(item) return sum(myList) def stack(*args): myList = list(args) myList = [item for item in myList if (item != None)] return sum(myList)'code' – user19605 Jun 30 '13 at 6:24
  • I'll try and run a more complete test. Perhaps you can edit your question to include the additional test(s) you ran and the errors that resulted from each test. – PolyGeo Jun 30 '13 at 6:59
  • just a general python note: if item is the same as if (item is not None) and easier to read. Likewise if not item is the same as if 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. – matt wilkie Jun 17 '15 at 21:04

I just ran the test below in the Field Calculator and it seemed to work.

def stack(item1,item2,item3):
  itemList = [item1,item2,item3]
  myList = [item for item in itemList if (item != None)]
  return sum(myList)

This was the Field Calculator settings I used:

enter image description here

and this is the test table with the result:

enter image description here

I think the only thing that I am not doing at the moment is joining through to another table but before I test that (I am using ArcGIS 10.1 SP1) perhaps you can confirm that this simple test is working for you. It's not as Pythonic but seems to work.


There appears to be a problem with either earlier versions of Arc (I use 10.0) or some installations of Arc where anytime your Python script sees a <Null>, <Null> is returned. I am aware of the if myfield is None approach, but this did not work for me, or a commenter here, or several other users with similar problems in gis.stackexchange. No matter the various clever manipulations/if-elifs, the field is set to <Null> if a <Null> is encountered at all in the script.

The only solution that works for me is to use VB Script instead of Python. The isNull(myfield) function reliably checks if a field is <Null> without any of the problems above. It is not too difficult to translate simple Python to VB Script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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