Multiple ways to remove the empty string from a list of strings in Python

This tutorial shows remove empty string space from a list in python with code examples

For example, Given list is [“one”,“two”,“three”,"",“four”], the output is [“one”,“two”,“three”,“four”]

Python to delete an empty string from a list of strings

There are multiple ways we can delete an empty string from a list.

  • use filter function filter method iterates the list and accepts None and list, returns the iterator to return list. It is easy and simple, and faster.
result = filter(None, names)
  • list Comprehensions syntax. List comprehension contains brackets, followed by for clause, and followed by zero or more if clause.

It returns a new list from following

  • Iterate each element in a list
  • Check element using if clause
  • assign the element to brackets

Here is an example

result1=[name for name in names if name]
  • lambda expression lambda expressions are expensive compared with other approaches. lambda used in filter makes the iterable list object, convert iterable to list using list() constructor.

Here is an example

results2=filter(lambda item: len(item)>0, names)


Learned multiple ways to remove the empty string from a list.