python – How to remove empty and whitespace-only elements from the list?

Question:

I have a list:

['', 'apple', '', ' ', 'banana', '', ' ', 'pine', '', ' ', 'price', '', ' ']

How do I remove '' and ' ' from the list?

Answer:

in functional style:

res = list(filter(len, map(str.strip, lst)))

print(res)
>>> ['apple', 'banana', 'pine', 'price']
Scroll to Top