Get first non-empty string from a list in python

前端 未结 6 2319
轻奢々
轻奢々 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 22:57

    Based on your question I'll have to assume a lot, but to "get" the first non-empty string:

    (i for i, s in enumerate(x) if s).next()
    

    which returns its index in the list. The 'x' binding points to your list of strings.

提交回复
热议问题