Reputation: 11
I have a long list of information scraped from the Billboard Hot 100 page which I am trying to distill into just songs and artists. However, I can't seem to remove all the empty parts as well as points in the list that are just blank spaces. '' and ' '
I have tried everything I can find on here with regards to the filter function as well as other options people have suggested. My initial attempt was using the included code, which worked for the first part of the list but for some reason never finishes and always stops part way.
for y in parse:
if y == "":
parse.remove("")
elif y == " ":
parse.remove(" ")
I would expect to be receiving a list with no '' or ' ' by themselves, but only the first part of the list is affected.
Upvotes: 0
Views: 104
Reputation: 16772
parse = ['a', ' ', 'b', 'c', '', 'd']
Python 2.x:
print(filter(str.strip, parse))
Python 3.x:
print(list(filter(str.strip, parse)))
OUTPUT:
['a', 'b', 'c', 'd']
EDIT:
To remove the empty spaces within the elements, let's say:
parse = ['a ', ' ', ' b', 'c', '', 'd ']
Using map()
with filter
and str.strip
:
Python 2.x:
print(map(str.strip, filter(str.strip, parse)))
Python 3.x:
print(list(map(str.strip, filter(str.strip, parse))))
OUTPUT:
['a', 'b', 'c', 'd']
Upvotes: 2
Reputation: 16997
you could use:
list1 = ["1 ", "", " ", "3", " 4", " ",""]
list2 = [x for x in list1 if x.strip() ]
output list2:
['1 ', '3', ' 4']
Upvotes: 0
Reputation: 5372
This will do the job:
>>> dirty =['1',' ', '', '2','3','4']
>>> clean = [row for row in dirty if row.strip()]
>>> clean
['1', '2', '3', '4']
>>>
Upvotes: 4
Reputation: 3054
Dont modify the list you are iterating over.
Assuming parse is like
parse=[1,' ', '', 2,3,4]
you can do something like
parse_fix=[]
for y in parse:
if y!='' and y!=' ': parse_fix.append(y)
then parse_fix
will be the list you want
the short version might look like this
parse=[y for y in parse if y!='' and y!=' ']
Upvotes: 1