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.

4
  • 4
    Post a sample of parse. Commented Apr 3, 2019 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 ['', ' ']] Commented Apr 3, 2019 at 14:53
  • 1
    I guess this can do the job: clean_list = [row for row in parse if row.strip()] Commented Apr 3, 2019 at 14:53
  • Possible duplicate of Python: Removing spaces from list objects Commented Apr 3, 2019 at 14:54

4 Answers 4

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']
>>> 
Sign up to request clarification or add additional context in comments.

2 Comments

I see what you did there. dirty
the if row.strip() bit is clever
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']

Comments

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!=' ']

3 Comments

Can't say because we're not sure of how exactly is the parse list in OP's case.
@DirtyBit well thats what the "assuming" bit was for
This worked! With one caveat - it needs to use 'and' instead of 'or'. Still not sure I understand why just modifying the list doesn't work, but now I know.
0

you could use:

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

output list2:

['1 ', '3', '  4']

2 Comments

That is exactly the answer I gave, @Frenchy.
yes ,just a problem of bandwith so i let you the paternity!! and i upvote to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.