Currently I use this helper function to remove the empty entries.
Is there a built-in way for this?
def getNonEmptyList(str, splitSym):
lst=str.split(splitSym)
lst1=[]
for entry in lst:
if entry.strip() !='':
lst1.append(entry)
return lst1
str.split(sep=None, maxsplit=-1)
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
For example:
>>> '1 2 3'.split()
['1', '2', '3']
>>> '1 2 3'.split(maxsplit=1)
['1', '2 3']
>>> ' 1 2 3 '.split()
['1', '2', '3']
This split could be done more compactly with a comprehension like:
def getNonEmptyList(str, splitSym):
return [s for s in str.split(splitSym) if s.strip() != '']
You could use filter
def get_non_empty_list(s, delimiter):
return list(filter(str.strip, s.split(delimiter)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With