For example I have a sentence
"He is so .... cool!"
Then I remove all the punctuation and make it in a list.
["He", "is", "so", "", "cool"]
How do I remove or ignore the empty string?
listpython
For example I have a sentence
"He is so .... cool!"
Then I remove all the punctuation and make it in a list.
["He", "is", "so", "", "cool"]
How do I remove or ignore the empty string?
Best Answer
You can use
filter
, withNone
as the key function, which filters out all elements which areFalse
ish (including empty strings)Note however, that
filter
returns a list in Python 2, but a generator in Python 3. You will need to convert it into a list in Python 3, or use the list comprehension solution.False
ish values include: