I am using this code to separate words into a list. The while loop is used to remove any blank spaces that come up, they will be elements with just a ''. The problem is even after I run the while loop there are still elements with just ''. I believe they are due to whitespaces and indentations. The while loop does get rid of about 2/3 of these spaces. Is there a way just to get the words separated? I don't want any blank elements because when I run a loop on them later I get a string index out of range when I reference mylist[i][0].
str = fpin.read()
mylist = re.split('[ \n]', str)
i = 0
while(i < len(mylist)):
if mylist[i] == '':
del mylist[i]
i = i + 1
.split()will eat up multiple whitespace characters, so you don't need this, but here's an example of how to make a copy with just what you want:newlist = [x for x in mylist if x]