views:

1029

answers:

6

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?

+3  A: 

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.

sykora
one line FTW! I love it.
Randolpho
A bit inefficient, though! If you have one million non-empty stings after just an empty one, you'll spend time and memory generating a list of one million string, only to get the first one... At least, use a generator!
Mapio
+3  A: 
def get_nonempty(list_of_strings):
    for s in list_of_strings:
        if s:
            return s
SilentGhost
Far less elegent than the Generator comprehension suggested above, also adds more complexity to the soruce file.
Daniel Goldberg
elegance in the eyes of the beholder, Daniel. It works flawlessly unlike the accepted answer's version: that's what matters
SilentGhost
+7  A: 
next(s for s in list_of_string if s)

Edit: py3k proof version as advised by Stephan202 in comments, thanks.

wuub
`[]` is called list comprehension.
SilentGhost
wouldn't work in py3k
SilentGhost
py3k version: `next(s for s in list_of_string if s)`.
Stephan202
I get an error (see below) with the py3k version, but .next() works. NameError: global name 'next' is not defined
Simon D
@Fred, next was made built-in in 2.6
SilentGhost
@Fred: indeed, this works since 2.6. In python 2.5 and prior you'll have to use `(...).next()`.
Stephan202
btw, it'll raise `StopIteration` if all strings are empty.
SilentGhost
@Fred: It's all very confusing really: next(generator) works in 2.6 and 3.x; generator.__next__() works in 3.x; generator.next() works in 2.x
John Machin
A: 

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
ghostdog74
+1  A: 

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()
Steve Losh
Why is the second way better?
Simon D
This only works in Python 2.x, and is less efficient than the generator example, because `filter` iterates over the whole list, irrespective of contents. In py3k `filter` returns a filter object, which is unsubscriptable.
Stephan202
@Fred:The first way will go through and filter out every empty string, create a new list, and select the first result.The second way will step through the list filtering out empty strings until it finds the first empty one and will return that. No intermediate list would be created.For example, if you have a list of 10,000 strings and only the first one is empty, the first method creates a new list of 9,999 strings in memory while the second does not.
Steve Losh
@Stephan202: The "slightly longer way" is equivalent to the generator example, I think.
Steve Losh
+1 for an answer that works in Python 2.x and 1.5.2 :-) -- unfortunately not in 3.x
John Machin
@Steve. Indeed. The py3k equivalent is `next(filter(None, list_of_strings))`. (No import required.)
Stephan202
A: 

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.