Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

How to remove an empty string from a Python list?

remove an empty string from a Python list

In this tutorial, you will learn how to remove empty and false values from a list in Python.  You can use Python’s built-in function filter(function, iterable), with None as the key function, which filters out all elements which are False.

# removing empty strings using filter()

# initializing list
programming_languages = ["", "Javascript", "", "Python", "PHP", ""]

# Printing original list
print ("Original list is : " + str(programming_languages))

# use filter() to remove False values. 
programming_languages = list(filter(None, programming_languages))
	
# Print filtered list
print ("Filtered list is : " + str(programming_languages))