Categories
Python Answers

How to remove empty strings from a list of strings with Python?

Spread the love

Sometimes, we want to remove empty strings from a list of strings with Python.

In this article, we’ll look at how to remove empty strings from a list of strings with Python.

How to remove empty strings from a list of strings with Python?

To remove empty strings from a list of strings with Python, we can call the filter function.

For instance, we write

str_list = list(filter(None, str_list))

to call filter with None and str_list to return an iterator with the strings in str_list that aren’t empty.

Then we convert the iterator to a list with list.

Conclusion

To remove empty strings from a list of strings with Python, we can call the filter function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *