My script gives 2 string lists, a and b . I want to convert them to float lists to do some calculations, but the lists have some gaps. For example, i have  a = ["1"," ","3","4"] and i want to fill those gaps with 0 and have this a = ["1","0","3","4"]
            Asked
            
        
        
            Active
            
        
            Viewed 1,856 times
        
    -1
            
            
         
    
    
        WilsonNeon
        
- 1
- 3
- 
                    7`[1, ,3,4]` does not define a list. – Moses Koledoye Jun 26 '17 at 08:53
- 
                    @MosesKoledoye Pretty sure he meant "1"," ","3","4". You should edit Wilson. – kabanus Jun 26 '17 at 08:53
- 
                    Where did the 'gaps' (empty strings?) come from, maybe you can stop them arising in the list in the first place – Chris_Rands Jun 26 '17 at 08:59
2 Answers
1
            
            
        Using a list-comprehension.
a = ["1", " ", "3", "4"]
[float(i) if i.strip() else 0. for i in a]
# [1.0, 0.0, 3.0, 4.0]
The strip() part is to make " " String evaluated to False.
 >>> bool("")
 False
 >>> bool("   ")
 True
 >>> bool("   ".strip())
 False
 
    
    
        Kruupös
        
- 5,097
- 3
- 27
- 43
