Get first non-empty string from a list in python

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

    Here's a short way:

    filter(None, list_of_strings)[0]
    

    EDIT:

    Here's a slightly longer way that is better:

    from itertools import ifilter
    ifilter(None, list_of_strings).next()
    

提交回复
热议问题