-2

['B', 'BA', 'BAN', 'BANA', 'BANAN', 'BANANA', '', '', 'N', 'NA', 'NAN', 'NANA', '', '', '', '', 'N', 'NA']

I was trying to remove these blanks ' ' but not all the blank spaces are getting removed by the loop. Only blanks in the starting of the list are getting removed.

I used this:

for i in range(0, len(list_for_kevin)):
    if list_for_kevin[i] == '':
        list_for_kevin.remove(list_for_kevin[i])

but after using this I got the output of the following:

enter image description here

Please Help me to find the right way to remove all the ' ' from the list.

Rishi Raj
  • 41
  • 8

1 Answers1

0

I hope this will solve your problem:

list_for_kevin = ['a', 'b', '', '', '', '', 'c']

list_for_kevin = [x for x in list_for_kevin if x != '']

print(list_for_kevin)

Sample output:

['a', 'b', 'c']
biqarboy
  • 841
  • 6
  • 15
  • It still doesn't solve my problem. Have you tried it on my list? – Rishi Raj Feb 05 '21 at 18:40
  • 1
    @RishiRaj You mean your list that you only provided as *image* so we *can't* easily try it? (Don't ever do that) – superb rain Feb 05 '21 at 18:44
  • 1
    @RishiRaj please add your input in text format in your post. Thanks. – biqarboy Feb 05 '21 at 20:13
  • 1
    @RishiRaj I understand and fix the issue in my code. Please follow the corresponding [this post](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) (where it is already been answered) for more details understanding about the problem. I am sorry not to check this case earlier. – biqarboy Feb 05 '21 at 20:39