Function that splits by delimiter, removes numerical only values and whitespaces or empty indices

I’m writing a function that splits by delimiter, removes numerical only values and whitespaces or empty indices.
However I can’t seem to get it to not print, or remove the empty indices that were split the the delimiter.

Say my sample is ABC//DEF/GH//I, I want it to split by "/" then remove the empty space that’s produced.
My output looks like this so far.

["ABC", "", "DEF", "GH", "", "I"]

What can I include to remove the "", bits?

def split_lines(lines, delimiter, remove = '[0-9]+$'):
  for line in lines:
    tokens = line.split(delimiter)
    tokens = [re.sub(remove, "", token) for token in tokens]
    print(tokens)

>Solution :

Try this one

lst = ["ABC", "", "DEF", "GH", "", "I"]


new_lst = list(filter(lambda e:e,lst))
print(new_lst)

OUTPUT

['ABC', 'DEF', 'GH', 'I']

If also want to remove ' ' a single space from the list then use this one

lst = ["ABC", " ", "DEF", "GH", " ", "I"]


new_lst = list(filter(lambda e:e.strip(),lst))
print(new_lst)

OUTPUT

['ABC', 'DEF', 'GH', 'I']

Leave a Reply