it seems to be a very basic question but I can't find an answer.
I'm trying to build a list of strings from a split on a bigger string.
input = 'I#have#a#problem'
result = [s for s in input.split('#')]
>>> ['I', 'have', 'a', 'problem']
That works perfectly. The problem is, sometimes the input is not a string, but None. To avoid the python error AttributeError: AttributeError: 'NoneType' object has no attribute 'split'
, I tried to add an if
statement, but that doesn't do anything to prevent the error.
input = None
result = [s for s in input.split('#') if input]
>>> AttributeError: 'NoneType' object has no attribute 'split'
Is there a way to do that while keeping the one-liner ?
Thanks