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
2009-07-16 14:33:08
one line FTW! I love it.
Randolpho
2009-07-16 14:35:00
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
2009-07-16 15:06:54
+3
A:
def get_nonempty(list_of_strings):
for s in list_of_strings:
if s:
return s
SilentGhost
2009-07-16 14:33:58
Far less elegent than the Generator comprehension suggested above, also adds more complexity to the soruce file.
Daniel Goldberg
2009-07-18 13:23:39
elegance in the eyes of the beholder, Daniel. It works flawlessly unlike the accepted answer's version: that's what matters
SilentGhost
2009-07-19 21:44:29
+7
A:
next(s for s in list_of_string if s)
Edit: py3k proof version as advised by Stephan202 in comments, thanks.
wuub
2009-07-16 14:35:12
I get an error (see below) with the py3k version, but .next() works. NameError: global name 'next' is not defined
Simon D
2009-07-16 14:55:20
@Fred: indeed, this works since 2.6. In python 2.5 and prior you'll have to use `(...).next()`.
Stephan202
2009-07-16 15:07:12
@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
2009-07-16 15:27:56
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
2009-07-16 14:47:29
+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
2009-07-16 15:00:57
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
2009-07-16 15:10:34
@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
2009-07-16 15:12:10
@Stephan202: The "slightly longer way" is equivalent to the generator example, I think.
Steve Losh
2009-07-16 15:14:48
+1 for an answer that works in Python 2.x and 1.5.2 :-) -- unfortunately not in 3.x
John Machin
2009-07-16 15:16:58
@Steve. Indeed. The py3k equivalent is `next(filter(None, list_of_strings))`. (No import required.)
Stephan202
2009-07-16 15:19:41