7

My list is like this,

mylist=[ " ","abc","bgt","llko","    ","hhyt","  ","      ","iuyt"]

How to remove all spaces containing strings inside this list?

no1
  • 717
  • 2
  • 8
  • 21
  • 1
    By empty do you mean you include strings containing only spaces? – edsioufi Aug 29 '13 at 09:06
  • Ya...My program reads a file and takes each line and splits.. so in list it has empty strings, i mean spaces – no1 Aug 29 '13 at 09:09
  • 1
    I don't know why noone suggested this, but the following seems to be the fastest among the top answers - `[elem for elem in mylist if not elem.isspace()]` – Sukrit Kalra Sep 03 '13 at 07:35
  • isspace() is supported? I didnt know... Thank you between!! – no1 Sep 03 '13 at 09:09

10 Answers10

17

You could use a list comprehension:

 new_list = [elem for elem in mylist if elem.strip()]

Using strip() guarantees that even strings containing only multiple spaces will be removed.

edsioufi
  • 8,297
  • 3
  • 36
  • 42
9

Use filter with unbound method str.strip.

Python 2.x

>>> mylist=[ " ","abc","bgt","llko"," ","hhyt"," "," ","iuyt"]
>>> filter(str.strip, mylist)
['abc', 'bgt', 'llko', 'hhyt', 'iuyt']

Python 3.x

>>> mylist=[ " ","abc","bgt","llko"," ","hhyt"," "," ","iuyt"]
>>> list(filter(str.strip, mylist))
['abc', 'bgt', 'llko', 'hhyt', 'iuyt']
falsetru
  • 357,413
  • 63
  • 732
  • 636
4

Just use filter with None.

filter(None, mylist)

If by empty strings you mean strings containing only whitespace characters, then you should use:

filter(str.strip, mylist)

Examples:

>>> filter(None, ['', 'abc', 'bgt', 'llko', '', 'hhyt', '', '', 'iuyt'])
['abc', 'bgt', 'llko', 'hhyt', 'iuyt']
>>> filter(str.strip, [' ', 'abc', 'bgt', 'llko', ' ', 'hhyt', ' ', ' ', 'iuyt'])
['abc', 'bgt', 'llko', 'hhyt', 'iuyt']
Viktor Kerkez
  • 45,070
  • 12
  • 104
  • 85
2

Try to use filter(lambda x: x.strip(), mylist):

>>> mylist=[ " ","abc","bgt","llko"," ","hhyt"," "," ","iuyt"]
>>>
>>> filter(lambda x: x.strip(), mylist)
['abc', 'bgt', 'llko', 'hhyt', 'iuyt']
>>>
>>> mylist=[ " ","abc","bgt","llko","    ","hhyt","  ","      ","iuyt"]
>>>
>>> filter(lambda x: x.strip(), mylist)
['abc', 'bgt', 'llko', 'hhyt', 'iuyt']
>>>
2
mylist = [x for x in [ " ","abc","bgt","llko"," ","hhyt"," "," ","iuyt"] if x]

... List comprehension with "if" clause and, in this case, relying on the fact that Python considers empty strings (and empty containers) to be "False" in boolean contexts.

If by "empty" you mean zero-length or containing only spaces then you can change the if clause to read if x.strip()

Jim Dennis
  • 17,054
  • 13
  • 68
  • 116
1

I would do it this way:

>>> mylist=[ " ","abc","bgt","llko"," ","hhyt"," "," ","iuyt"]
>>> new_list = [e for e in mylist if len(e.strip())!=0]
>>> new_list
      ['abc', 'bgt', 'llko', 'hhyt', 'iuyt']
crs17
  • 541
  • 2
  • 6
1

The method isalpha() checks whether the string consists of alphabetic characters only:

mylist=[ " ","abc","bgt","llko"," ","hhyt"," "," ","iuyt"] 
mylist = [word for word in mylist if word.isalpha()]
print mylist

Output:['abc', 'bgt', 'llko', 'hhyt', 'iuyt']
badc0re
  • 3,333
  • 6
  • 30
  • 46
0
mylist = [s for s in mylist if str is not " "]
Henrik
  • 4,254
  • 15
  • 28
0
list = ["first", "", "second"]
[x for x in list if x]

Output: ['first', 'second']

Shortened as suggested,the same question given below

Remove empty strings from a list of strings

Community
  • 1
  • 1
kondapaka
  • 132
  • 1
  • 10
0
>>> mylist=[ " ","abc","bgt","llko"," ","hhyt"," "," ","iuyt"]
>>> [i for i in mylist if i.strip() != '']
['abc', 'bgt', 'llko', 'hhyt', 'iuyt']
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42