How to remove empty strings from a list of strings in Python

4 Answers

0 votes
lst = ["python", "", "", "java", "", "c#", ""] 
  
while("" in lst): 
    lst.remove("") 
      
print(lst) 



'''
run:

['python', 'java', 'c#']

'''

 



answered Jan 5, 2020 by avibootz
edited Jun 6, 2021 by avibootz
0 votes
lst = ["python", "", "", "java", "", "c#", ""] 
   
lst = [s for s in lst if s] 
       
print(lst) 
 
 
 
'''
run:
 
['python', 'java', 'c#']
 
'''

 



answered Jan 5, 2020 by avibootz
edited Jun 6, 2021 by avibootz
0 votes
lst = ["python", "", "", "java", "", "c#", ""] 
   
lst = ' '.join(lst).split() 
       
print(lst) 
 
 
 
'''
run:
 
['python', 'java', 'c#']
 
'''

 



answered Jan 5, 2020 by avibootz
edited Jun 6, 2021 by avibootz
0 votes
lst = ["python", "", "", "java", "", "c#", ""] 
    
lst = list(filter(None, lst))
        
print(lst) 

  
  
'''
run:
  
['python', 'java', 'c#']
  
'''

 



answered Jun 6, 2021 by avibootz

Related questions

2 answers 642 views
1 answer 187 views
1 answer 177 views
2 answers 318 views
1 answer 203 views
...