Get first non-empty string from a list in python

前端 未结 6 2230
轻奢々
轻奢々 2021-02-13 22:30

In Python I have a list of strings, some of which may be the empty string. What\'s the best way to get the first non-empty string?

6条回答
  •  青春惊慌失措
    2021-02-13 23:20

    To remove all empty strings,

    [s for s in list_of_strings if s]

    To get the first non-empty string, simply create this list and get the first element, or use the lazy method as suggested by wuub.

提交回复
热议问题