Python Practice › String Programs › Program 53
Remove blank strings from a list
Write a Python program to remove blank strings from a list of strings.
Input
["Python", " ", " ", "sqatools"]
Output
['Python', 'sqatools']
Approach
- Keep items whose stripped text is not empty.
Solution
items = ["Python", " ", " ", "sqatools"]print([s for s in items if s.strip()])Output
['Python', 'sqatools']
Line by line
Line 2
" ".strip() is "", which is falsy, so blank items are dropped.
Learn the concept
New to this topic? Read the lesson, then come back.