Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all spaces in the strings in list

Tags:

python

My list is like this,

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

How to remove all spaces containing strings inside this list?

like image 395
no1 Avatar asked Aug 29 '13 09:08

no1


1 Answers

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.

like image 99
edsioufi Avatar answered Sep 21 '22 17:09

edsioufi