Get first non-empty string from a list in python

前端 未结 6 2219
轻奢々
轻奢々 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-13 23:09

    to get the first non empty string in a list, you just have to loop over it and check if its not empty. that's all there is to it.

    arr = ['','',2,"one"]
    for i in arr:
        if i:
            print i
            break
    

提交回复
热议问题