How to remove empty string in a list?

前端 未结 9 1641
我在风中等你
我在风中等你 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:37

    Python 3 returns an iterator from filter, so should be wrapped in a call to list()

    str_list = list(filter(None, str_list)) # fastest
    
    0 讨论(0)
  • 2020-12-28 18:38
    lst = ["He", "is", "so", "", "cool"]
    lst = list(filter(str.strip, lst))
    
    0 讨论(0)
  • 2020-12-28 18:43

    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']
    
    0 讨论(0)
提交回复
热议问题