Remove Empty String from List in Python Example

04-Nov-2022

.

Admin

Remove Empty String from List in Python Example

Hi Dev,

In this post, we will learn Remove Empty String from List in Python Example. I would like to share with you Python Remove Empty Strings in a List. let’s discuss about How to Remove Empty String in Python. I would like to show you How to Remove Empty String From List in Python. Follow bellow tutorial step of Python Remove Empty String From List.

There are several ways to remove empty string values from the list in python. we will use filter(), join() and remove() functions to delete empty string from list. so let's see the below examples.

so let's see following examples with output:

Example 1:


main.py

myList = ['', 'Nicesnippets.com', '', 'is', 'Best', '']

# Remove Empty Value from List

newList = list(filter(None, myList))

print(newList)

Output:

['Nicesnippets.com', 'is', 'Best']

Example 2:

main.py

myList = ['', 'Nicesnippets.com', '', 'is', 'Best', '']

# Remove Empty Value from List

newList = ' '.join(myList).split()

print(newList)

Output:

['Nicesnippets.com', 'is', 'Best']

Example 3:

main.py

myList = ['', 'Nicesnippets.com', ' ', 'is', 'Best', '']

# Remove Empty Value from List

while("" in myList) :

myList.remove("")

print(myList)

Output:

['Nicesnippets.com', 'is', 'Best']

#Python