0

I am trying to remove white spaces from a list of strings in Python. I have tried almost every other method, but still I am not able to remove the spaces. Here is the list:

names=[': A slund\n', ': N Brenner and B Jessop and M Jones and G Macleod\n', ': C Boone\n', ': PB Evans\n', ': F Neil utitle: uThe architecture of markets}\n', ': PA Hall and D Soskice\n', '', '', '', '', '', '', '', ': EBYP HIGONNET and DS LANDES and H ROSOVSKY\n', '', '', '', '', '', '', ': DS Landes\n', '', '', '', '', '', '', '', ': DC North\n', '', '', '', '', '', '', '', ': K Polyani\n', '', '', '', '', '', '']

Here is my code:

for i in names:
    if len(i)== 0:   // i==''  // len(i)<=1
    names.remove(i)
print names
M.D
  • 97
  • 1
  • 9

1 Answers1

1

With list comprehension.

names_without_space = [name.replace(' ', '') for name in names]
print(names_without_space[:3])
# [':Aslund\n', ':NBrennerandBJessopandMJonesandGMacleod\n', ':CBoone\n']
Romain
  • 19,910
  • 6
  • 56
  • 65