Remove empty strings from a list of strings

后端 未结 12 1683
孤城傲影
孤城傲影 2020-11-22 04:33

I want to remove all empty strings from a list of strings in python.

My idea looks like this:

while \'\' in str_list:
    str_list.remove(\'\')
         


        
12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 05:11

    filter actually has a special option for this:

    filter(None, sequence)
    

    It will filter out all elements that evaluate to False. No need to use an actual callable here such as bool, len and so on.

    It's equally fast as map(bool, ...)

提交回复
热议问题