I am attempting to label features that can have multiple combinations of codes. The label is based off of three fields ([DescriptionA], [DescriptionB], [DescriptionC]).

I'd like to create a python expression to remove all null values from the labels, and to remove a section from the end of each returned value.

(Ex. 10QR, 15QR)

Here is the code I have come up with based on a similar thread I've read:

    def FindLabel ( [DescriptionA], [DescriptionB], [DescriptionC] ):
return def FindLabel ( [DescriptionA], [DescriptionB], [DescriptionC] ):
if not ([DescriptionC] is None):
    return [DescriptionC]  .lstrip('Fuse-')
elif not([DescriptionB] is None):
    return [DescriptionB]  .lstrip('Fuse-') + '\n' + [DescriptionA]   .lstrip('Fuse-')
elif not([DescriptionA] AND [DescriptionB]  AND [DescriptionC]  is None):
    return [DescriptionA]  .lstrip('Fuse-') + '\n' + [DescriptionB]   .lstrip('Fuse-') + '\n' + [DescriptionC]   .lstrip('Fuse-')
    return [DescriptionA]  .lstrip('Fuse-')

When I verify, it returns a syntax error message. I am sure there is more wrong to it than that. How should I proceed?


1 Answer 1


There's a few things wrong with your script:

  • First indentation, all lines post the def statement should be indented - python uses indentation for code levels; I'll assume that when you pasted into your question the indentation went funny otherwise you would have got an 'inconsistent indent' error rather than a syntax error.
  • Your first line returns the function. You should never see return def together. return ends a function and def starts one which would confuse the parser and is most likely to be where you're getting a syntax error.
  • You have multiple return statements, when you call return that is the end of your function, no more lines of code will be evaluated! Finished! All done. The way the logic flows you will return [DescriptionC] and nothing else if the field is not null. I would suggest building a list of the values (if not Null) and then returning on the last line (see example) but you could also build a string and return that, the key is do all the work and then return only when you're sure there's nothing else that could possibly be done.
  • You have a space between description and .lstrip, while this will not stop the code from working it makes it very hard to read.

I believe you're after something like this:

def FindLabel ( [DescriptionA], [DescriptionB], [DescriptionC] ):
    rVal = [] # empty return list

    # if any values aren't null add them to the return list, str() isn't neccesary 
    # because the values passed to this function are always strings but for
    # clarity I'm showing that this only works for strings.
    if [DescriptionA] != None:
        rVal.append(str([DescriptionA])) # add lstrip if needed
    if [DescriptionB] != None:           # to these append statements
        rVal.append(str([DescriptionB])) # rVal.append(str([field]).lstrip('string'))
    if [DescriptionC] != None:

    if len(rVal) == 0:
        return '' # empty string, all 3 fields are null
        return '({})'.format(', '.join(rVal)) # return in format '(A, B, C)' only works on string lists

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.