Select to view content in your preferred language

concatenation of items with null values

749
3
07-12-2023 04:51 PM
Bradmayger
Emerging Contributor

any idea why this code fails please ?

3 Replies
DanPatterson
MVP Esteemed Contributor

You don't seem to account for null at all but it is hard to read

Code formatting ... the Community Version - Esri Community

<null> in python is None not a space

if a not in [None, " ", ""]:
    # then do something

 

 


... sort of retired...
0 Kudos
RhettZufelt
MVP Notable Contributor

Also, I see the python function, but don't see you calling it:

RhettZufelt_0-1689256955934.png

 

You need to call update() and pass the variables to it.

R_

0 Kudos
JohannesLindner
MVP Frequent Contributor

Like the others have said, you actually have to call the function, and you should compare to None, not space.

x = [1, None, 2]
for y in x:
    print(y is None)

# False
# True
# False

 

Also, your function is needlessly complicated. Just use something like this:

# BFS_concat_trial = 
concat_if_not_empty([!Field1!, !Field2!, !Field3!, !Field4!, !Field5!, !Field6!], [None, "", " "], "/")

# code block
def concat_if_not_empty(values, empty, separator):
    """Concatenates non-empty values.
    values: list of values to concatenate
    empty: list of values considered empty
    separator: character used for concatenation
    """
    values = [str(v) for v in values if v not in empty]
    return separator.join(values)

JohannesLindner_0-1689279363636.png

 


Have a great day!
Johannes