For example I have a sentence
\"He is so .... cool!\"
Then I remove all the punctuation and make it in a list.
[\"He\", \"
Python 3 returns an iterator
from filter
, so should be wrapped in a call to list()
str_list = list(filter(None, str_list)) # fastest
lst = ["He", "is", "so", "", "cool"]
lst = list(filter(str.strip, lst))
You can filter out empty strings very easily using a list comprehension:
x = ["He", "is", "so", "", "cool"]
x = [str for str in x if str]
>>> ['He', 'is', 'so', 'cool']