Python Practice › String Programs › Program 53

Remove blank strings from a list

EasyString Programs · 53 of 106

Write a Python program to remove blank strings from a list of strings.

Advertisement
Input
["Python", " ", " ", "sqatools"]
Output
['Python', 'sqatools']

Approach

  1. Keep items whose stripped text is not empty.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs