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.

0

2 Answers 2

1
words = ['', 'int', '', 'main', '', '', '', '', '', '', 'return', '0', '']
while '' in words:
    words.remove('')
print(words)
Sign up to request clarification or add additional context in comments.

Comments

0

I would use a list comprehension:

[word for word in words if word != '']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.