Python Remove Empty String from List

Python remove empty string from list | We will discuss how to remove the empty string from a list using various methods. In Python, there are many methods available on the list data type that help you remove the empty strings from a given list. In this post, we are using native method, list comprehension, remove(), join(), split(), filter(), and strip() function.

Remove Empty String Elements from List in Python

We will take the list while declaring the variables then, the Python program removes the empty string from the list using the For Loop and if-else statement. Finally, the new list will be displayed on the screen.

# Python program to remove empty string from list

# take list
my_list = ['Know Program', '', 'Python', 'C', '', 'Java']

# printing original list
print('List:', my_list)

# remove empty string using native method
new_list = []
for i in my_list:
    if (i):
        new_list.append(i)

# printing list without empty string
print('New List:', new_list)

Output:-

List: [‘Know Program’, ”, ‘Python’, ‘C’, ”, ‘Java’]
New List: [‘Know Program’, ‘Python’, ‘C’, ‘Java’]

Using List Comprehension

This method is similar to the above method, but rather a shorthand method. In this program, we use the list comprehension technique. This is just a one-liner shorthand of the longer method.

# Python program to remove empty string from list

# take list
my_list = ['Know Program', '', 'Python', 'C', '', 'Java']

# printing original list
print('List:', my_list)

# remove empty string using list comprehension
new_list = [i for i in my_list if i]

# printing list without empty string
print('New List:', new_list)

Output:-

List: [‘Know Program’, ”, ‘Python’, ‘C’, ”, ‘Java’]
New List: [‘Know Program’, ‘Python’, ‘C’, ‘Java’]

Using Filter() Function

The filter() function returns an iterator where the items are filtered through a function to test if the item is accepted or not. This method is the most elegant and fastest way to perform.

# Python program to remove empty string from list

# take list
my_list = ['Know Program', '', 'Python', 'C', '', 'Java']

# printing original list
print('List:', my_list)

# remove empty string using filter()
new_list = list(filter(None, my_list))

# printing list without empty string
print('New List:', new_list)

Output:-

List: [‘Know Program’, ”, ‘Python’, ‘C’, ”, ‘Java’]
New List: [‘Know Program’, ‘Python’, ‘C’, ‘Java’]

Using remove() Function

The remove() method takes a single element as an argument and removes it from the list. remove() generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in the list.

# Python program to remove empty string from list

# take list
my_list = ['Know Program', '', 'Python', 'C', '', 'Java']

# printing original list
print('List:', my_list)

# remove empty string using remove()
while('' in my_list):
    my_list.remove('')

# printing list without empty string
print('New List:', my_list)

Output:-

List: [‘Know Program’, ”, ‘Python’, ‘C’, ”, ‘Java’]
New List: [‘Know Program’, ‘Python’, ‘C’, ‘Java’]

Python split Ignore Empty

The join() method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string, and tuple) by a string. The split() method breaks up a string at the specified separator and returns a list of strings. We first join all the strings so that empty space is removed, and then split it back to the list so that the new list made now has no empty string.

# Python program to remove empty string from list

# take list
my_list = ['Know Program', '', 'Python', 'C', '', 'Java']

# printing original list
print('List:', my_list)

# remove empty string using join() + split()
new_list = ' '.join(my_list).split()

# printing list without empty string
print('New List:', new_list)

Output:-

List: [‘Know Program’, ”, ‘Python’, ‘C’, ”, ‘Java’]
New List: [‘Know’, ‘Program’, ‘Python’, ‘C’, ‘Java’]

Using strip() Function

The strip() method returns a copy of the string by removing both the leading and the trailing characters (based on the string argument passed).

# Python program to remove empty string from list

# take list
my_list = ['Know Program', '', 'Python', 'C', '', 'Java']

# printing original list
print('List:', my_list)

# remove empty string using strip()
new_list = [x.strip() for x in my_list if x.strip()]

# printing list without empty string
print('New List:', new_list)

Output:-

List: [‘Know Program’, ”, ‘Python’, ‘C’, ”, ‘Java’]
New List: [‘Know Program’, ‘Python’, ‘C’, ‘Java’]

Also See:- Delete all Elements in List Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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