Search code examples
pythonpython-3.xlistreplacelemmatization

How to get rid of blanks in Python list


I have successfully managed to get rid of '\n' in a list, but I can't seem to get rid of a blank.

This is what I've done:

from pymystem3 import Mystem
m = Mystem()
user_flash = Flashcard.objects.filter(owner=self.request.user).values_list('question', flat=True)
user_flash_ = [item.replace('\n', ' ') for item in m.lemmatize(" ".join(user_flash))]

But when I try to do the same for "", it doesn't work.

So, before I did anything, the list looked something like: ['word1', '', 'word2', '\n'] After implementing my code above I'm left with: ['word1', '', 'word2'] How can I get rid of that one blank? The following...

user_flash_clean = [item.replace('', ' ') for item in user_flash_]

...didn't work...

I have found a way to circumvent the problem, but it would be nice if it's possible to just get rid of that blank. (Note - I do have a good reason for doing the .join before lemmatizing, but if that is where my problem is coming from, would be happy to explain why I did it, so that I can maybe implement a different solution)


Solution

  • You can filter the words in user_flash_ based on the length of their stripped value:

    user_flash_ = ['word1', '', 'word2', '\n']
    
    user_flash_clean = [w for w in user_flash_ if len(w.strip())]
    print(user_flash_clean)
    

    Output:

    ['word1', 'word2']
    

    Note that in a boolean context, strings are false if they are empty (''), so you don't actually need len in the above code:

    user_flash_clean = [w for w in user_flash_ if w.strip()]