Skip to content

How to remove an empty string from a Python list?

Last updated on August 8, 2022

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))

Comments are closed, but trackbacks and pingbacks are open.