24

For example I have a sentence

"He is so .... cool!"

Then I remove all the punctuation and make it in a list.

["He", "is", "so", "", "cool"]

How do I remove or ignore the empty string?

1
  • Just a reminder that some english words contain punctuation. Depending on how you intend to use this, you may need to take this into consideration. Commented Apr 19, 2013 at 8:08

9 Answers 9

52

You can use filter, with None as the key function, which filters out all elements which are Falseish (including empty strings)

>>> lst = ["He", "is", "so", "", "cool"]
>>> filter(None, lst)
['He', 'is', 'so', 'cool']

Note however, that filter returns a list in Python 2, but a generator in Python 3. You will need to convert it into a list in Python 3, or use the list comprehension solution.

Falseish values include:

False
None
0
''
[]
()
# and all other empty containers
Sign up to request clarification or add additional context in comments.

4 Comments

Be careful however, as this will return a list in Python 2 and a generator in Python 3.
what happens if you have ['']?
@MattO'Brien since the list has an element, it is non-empty, and therefore it is considered Trueish.
A False inside a True, eh
25

You can filter it like this

orig = ["He", "is", "so", "", "cool"]
result = [x for x in orig if x]

Or you can use filter. In python 3 filter returns a generator, thus list() turns it into a list. This works also in python 2.7

result = list(filter(None, orig))

Comments

7

You can use a list comprehension:

cleaned = [x for x in your_list if x]

Although I would use regex to extract the words:

>>> import re
>>> sentence = 'This is some cool sentence with,    spaces'
>>> re.findall(r'(\w+)', sentence)
['This', 'is', 'some', 'cool', 'sentence', 'with', 'spaces']

Comments

5

I'll give you the answer to the question you should have asked -- how to avoid the empty string altogether. I assume you do something like this to get your list:

>>> "He is so .... cool!".replace(".", "").split(" ")
['He', 'is', 'so', '', 'cool!']

The point is that you use .split(" ") to split on space characters. However, if you leave out the argument to split, this happens:

>>> "He is so .... cool!".replace(".", "").split()
['He', 'is', 'so', 'cool!']

Quoth the docs:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

So you really don't need to bother with the other answers (except Blender's, which is a totally different approach), because split can do the job for you!

Comments

2
>>> from string import punctuation
>>> text = "He is so .... cool!"
>>> [w.strip(punctuation) for w in text.split() if w.strip(punctuation)]
['He', 'is', 'so', 'cool']

2 Comments

In order to avoid calling w.strip(punctuation) twice, l = [w.strip(punctuation) for w in text.split()] and then [w for w in l if w] wuld be preferable...
@glglgl I'm okay with calling it twice in this case, but yes that is also an option. I would rather use a generator though
1

You can filter out empty strings very easily using a list comprehension:

x = ["He", "is", "so", "", "cool"]
x = [str for str in x if str]
>>> ['He', 'is', 'so', 'cool']

Comments

1

Python 3 returns an iterator from filter, so should be wrapped in a call to list()

str_list = list(filter(None, str_list)) # fastest

Comments

0
lst = ["He", "is", "so", "", "cool"]
lst = list(filter(str.strip, lst))

Comments

-1

You can do this with a filter.

a = ["He", "is", "so", "", "cool"]
filter(lambda s: len(s) > 0, a)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.