0

I want to remove all the spaces in the words list.

words = ['', 'int', '', 'main', '', '', '', '', '', '', 'return', '0', '']
for i in words:
    words.remove('')
print(words)

but this prints

['int', 'main', '', 'return', '0', '']

I want ['int', 'main', 'return', '0'] as the out put.

I am using Python3.

awpathum
  • 227
  • 4
  • 13

2 Answers2

1
words = ['', 'int', '', 'main', '', '', '', '', '', '', 'return', '0', '']
while '' in words:
    words.remove('')
print(words)
0

I would use a list comprehension:

[word for word in words if word != '']
Ben Taylor
  • 483
  • 2
  • 13