16

How do I delete empty strings from a list? I tried like this:

starring = ['Vashu Bhagnani', 'Khemchand Bhagnani', ' ', 'Jacky Bhagnani', ' ', 'Prashant Shah', ' ']
output = filter(bool, starring)

Output I want:

['Vashu Bhagnani', 'Khemchand Bhagnani',  'Jacky Bhagnani',  'Prashant Shah']

But output ends up being the same as input. What's the correct function to pass to filter?

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
  • 2
    [Your code works for me](http://codepad.org/BzqIe31y), what is your problem? – Felix Kling Dec 09 '11 at 17:39
  • 1
    possible duplicate of [Remove empty strings from a list of strings](http://stackoverflow.com/questions/3845423/remove-empty-strings-from-a-list-of-strings) – Blender Dec 09 '11 at 17:39
  • What's wrong with what you posted? It works for me. – Kos Dec 09 '11 at 17:39
  • 1
    @Felix Kling. Code update. There is a space in `' '`. –  Dec 09 '11 at 17:40
  • Spaces evaluate to True, so if you don't want spaces you'll have to write a custom comparison function that deals with that or use a list comprehension. Definitely look at the post that Blender mentioned. – Sanjamal Dec 09 '11 at 17:43
  • Does this answer your question? [Remove empty strings from a list of strings](https://stackoverflow.com/questions/3845423/remove-empty-strings-from-a-list-of-strings) – AMC Jan 09 '20 at 20:54

3 Answers3

25

Only the empty string evaluates to False so you need to use strip() to remove any whitespace and we can then rely on non-blank strings being evaluated as true.

>>> starring = ['Vashu Bhagnani', 'Khemchand Bhagnani', ' ', 'Jacky Bhagnani', ' ', 'Prashant Shah', ' ']                                      
>>> starring = filter(lambda name: name.strip(), starring)
>>> starring
['Vashu Bhagnani', 'Khemchand Bhagnani', 'Jacky Bhagnani', 'Prashant Shah']

Although a list comprehension might be easier:

>>> [name for name in starring if name.strip()]
['Vashu Bhagnani', 'Khemchand Bhagnani', 'Jacky Bhagnani', 'Prashant Shah']
David Webb
  • 190,537
  • 57
  • 313
  • 299
9

You can remove trailing and leading white spaces, which will result in an empty string if it only contains those.

List comprehension:

l = [x for x in l if x.strip()]

With filter and operator.methodcaller [docs]:

l = filter(operator.methodcaller('strip'), l)

or simpler:

l = filter(str.strip, l)

operator.methodcaller would be the only way if you'd want to pass additional arguments to the method.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    I like `l = filter(operator.methodcaller('strip'), l)` Thanks –  Dec 09 '11 at 17:47
  • Is using `operator.methodcaller('strip')` preferred over just doing `str.strip` (i.e., instead of doing `filter(str.strip, l)`)? – David Alber Dec 09 '11 at 17:47
  • 1
    @David: I didn't know that `str.strip` works but thinking about it, seems to be reasonable. Thanks! I guess you would use `operator.methodcaller` when you want to pass additional arguments. – Felix Kling Dec 09 '11 at 17:50
  • Also `operator.methodcaller` will not fail on unicode strings and other stuff in the list, i.e. it is better suited for pythonic duck-typing model. – Tigran Saluev Jun 25 '14 at 11:50
0
s = ['Vashu Bhagnani', 'Khemchand Bhagnani', '', 'Jacky Bhagnani', '', 'Prashant Shah', '']
[a for a in s if len(a) > 0]
ceth
  • 44,198
  • 62
  • 180
  • 289