0

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.

Mark
  • 90,562
  • 7
  • 108
  • 148
Bryan
  • 11
  • 1
  • 4
    Post a sample of `parse`. – DirtyBit Apr 03 '19 at 14:51
  • You are modifying the list as you iterating over it, this will cause items to be skipped. You should use something like a list comprehension or filter for this. For example: `parse = [p for p in parse if p not in ['', ' ']]` – Mark Apr 03 '19 at 14:53
  • 1
    I guess this can do the job: ```clean_list = [row for row in parse if row.strip()]``` – accdias Apr 03 '19 at 14:53
  • Possible duplicate of [Python: Removing spaces from list objects](https://stackoverflow.com/questions/3232953/python-removing-spaces-from-list-objects) – sniperd Apr 03 '19 at 14:54

4 Answers4

4

This will do the job:

>>> dirty =['1',' ', '', '2','3','4']
>>> clean = [row for row in dirty if row.strip()]
>>> clean
['1', '2', '3', '4']
>>> 
accdias
  • 5,160
  • 3
  • 19
  • 31
2

Using filter with str.strip:

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']
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
1

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!=' ']
SuperStew
  • 2,857
  • 2
  • 15
  • 27
0

you could use:

list1 = ["1 ", "", " ", "3", "  4", "       ",""]
list2 = [x for x in list1 if x.strip() ]

output list2:

['1 ', '3', '  4']
Frenchy
  • 16,386
  • 3
  • 16
  • 39