A = ["a","b"," ","c"," ","d"] How can I remove str. Of white spaces from this list
Asked
Active
Viewed 61 times
2 Answers
1
A = [s for s in A if s.strip()]
The above code works because empty strings - ie '' - evaluate to False.

spencer.pinegar
- 442
- 2
- 10
0
A = [s for s in A if s.strip() != '']
or
A = [s for s in A if len(s.strip()) > 0]

William
- 16
- 1
-
2Because empty strings '' evaluate to false you can simplify even more; A = [s for s in A if s.strip()] – spencer.pinegar Jul 28 '21 at 23:21