How to remove empty string in a list?

前端 未结 9 1669
我在风中等你
我在风中等你 2020-12-28 17:42

For example I have a sentence

\"He is so .... cool!\"

Then I remove all the punctuation and make it in a list.

[\"He\", \"         


        
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 18:34

    You can filter it like this

    orig = ["He", "is", "so", "", "cool"]
    result = [x for x in orig if x]
    

    Or you can use filter. In python 3 filter returns a generator, thus list() turns it into a list. This works also in python 2.7

    result = list(filter(None, orig))
    

提交回复
热议问题