I am unsure what syntax issue I am having with my If/Elif statement. I want to not use a column in my calculation if the value is blank. The end goal is to not have extra spaces before, after and between values.

Please be kind. I am still learning Python for scripting in ArcGIS Desktop.

Here is my Prelogic and Expression.


def ifFields(SubAdd,LandM, ZipN):
  if SubAdd== " ":
    return "!LandmarkName! + ' ' + '('+ !ZipName! + ')'"
  elif LandM== " ":
    return "!SubAddress! + ' ' + '('+ !ZipName!+')'"
  elif ZipN== " ":
    return "!SubAddress! + ' ' + !LandmarkName!"


ifField(!SubAddress!, !LandmarkName!, !ZipName!)
  • 2
    What is your desired return? Could you give an example? I dont think it will return what you actually want – BERA Dec 7 '17 at 20:33
  • What my desired result are, the columns may have blanks and I do not want extra spaces after my concatenation. So if there are I want to populate the returned results as listed. – Randy Wolter Dec 7 '17 at 21:42
  • Don't forget to take the Tour which leads you through to advice on how to structure questions at gis.meta.stackexchange.com/a/3353/115 Your title should only summarize your question body, while your question body should contain all information pertinent to telling us what you want to do, what you have tried and where you are stuck. – PolyGeo Dec 7 '17 at 23:06
  • It's not just best practice to set a value within the if block, then return it at the end, it's effectively required within inline helper functions. In addition, use the values passed into the function (the !var! is not valid in the code). – Vince Dec 7 '17 at 23:58
  • What results are you currently getting that are not desired? And, again, what are your desired results? – MaryBeth Dec 8 '17 at 20:03

You define the function as ifFields but call it with ifField. They need to be the same.

Also I dont think it will return what you want.

Try changing:

"!LandmarkName! + ' ' + '('+ !ZipName! + ')'"


'{0} ({1})'.format(LandM, ZipN)

And the same way for the elifs. If you dont want it to return None if none of the conditions are met, you need to end with an else.

  • Dang, I thought I fixed that ifFields issue. Good eye! – Randy Wolter Dec 7 '17 at 21:42
  • @RandyWolter if my answer answered your question could you accept it with the checkbox? – BERA Dec 8 '17 at 14:24

In addition to what was mentioned by BERA, you are also returning the field names as text, and referencing the field names, not the variable names. You code should look more like this:

def ifFields(SubAdd,LandM, ZipN):
  if SubAdd== " ":
    return LandM + ' ' + '('+ ZipN + ')'
  elif LandM== " ":
    return SubAdd + ' ' + '('+ ZipN+')'
  elif ZipN== " ":
    return SubAdd + ' ' + LandM

However this is not very nice and can cause issues when dealing with numbers. Better to use:

def ifFields(SubAdd,LandM, ZipN):
  if SubAdd== " ":
    return '{0} ({1})'.format(LandM, ZipN)
  elif LandM== " ":
    return '{0} ({1})'.format(SubAdd, ZipN)
  elif ZipN== " ":
    return '{0} {1}'.format(SubAdd, LandM)

That should do something and not error, but I am still unsure what your goal is. It might be better to use str.join on all the ones that exist, but again, I don't fully understand what your goal is here. If you have more than one thing blank this is going to get ugly. Also blanks can be None or '', so it might be good to check for those too.

  • Great tip. Will there be a space between the two field values? Also, I need the ZipN field to be in parenthesis. I'll test it out. Thanks. – Randy Wolter Dec 7 '17 at 21:44
  • Yes it will add the space. And parenthesis around the ZipN. Please read more about str.format here: docs.python.org/2/library/… – isamson Dec 7 '17 at 22:49
  • I want to add a forth line to the elif statement. I think it needs to be a else. Any suggestions to what I am doing wrong? 'def ifFields(SubAdd, LandM, ZipN): if SubAdd== "": return '{0} ({1})'.format(LandM, ZipN) elif LandM== "": return '{0} ({1})'.format(SubAdd, ZipN) elif ZipN== "": return '{0} {1}'.format(SubAdd, LandM) elif SubAdd=="" and LandM== "": return '(ZipN)' else: return '{0} {1} ({2})'.format(LandM, SubAdd, ZipN)' @isamson – Randy Wolter Feb 9 '18 at 21:47
  • Sorry. I can't get it to format correctly. – Randy Wolter Feb 9 '18 at 21:52
  • def ifFields(SubAdd, LandM, ZipN): if (SubAdd == "" and LandM == ""): return '({0})'.format(ZipN) elif (SubAdd <> "" and LandM <> ""): return '{0} {1} ({2})'.format(SubAdd, LandM, ZipN) elif (SubAdd == "" and LandM <> ""): return '{0} ({1})'.format(LandM, ZipN) elif (SubAdd <> "" and LandM == ""): return '{0} ({1})'.format(SubAdd, ZipN) – Randy Wolter Feb 12 '18 at 19:19

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.