Home Python How To Remove Empty Strings From a List Of Strings

How To Remove Empty Strings From a List Of Strings

0
How To Remove Empty Strings From a List Of Strings
String Index Out Of Range Solution

On this page, we will discuss how to remove empty strings from a list of strings. It’s a date object which stores some space in the memory.

A Python list is a sequential collection of data objects and empty strings as well. Even if it represents a false value in Python. We have described the best 4 ways to kick out this error.

Let’s get started!

Contents

Remove Empty Strings From a List Of Strings

  1. Using remove()
  2. Using List Comprehension
  3. Use join() + split()
  4. Using filter()

Process to use remove() function

The remove() function is very easy to use, some people are not recommending to use this, but it performs the task well. It generally removes the first empty string and keeps doing iterate this process until no empty string is found in the list.

test_list = ["", "Wholeblogs", "", "is", "best", ""]
 
# Printing original list
print ("Original list is : " + str(test_list))
 
# using remove() to
# perform removal
while("" in test_list) :
	test_list.remove("")
 
# Printing modified list
print ("Modified list is : " + str(test_list))

Output:

Original list is : [”, ‘Wholeblogs’, ”, ‘is’, ‘best’, ”]

Modified list is : [‘Wholeblogs’, ‘is’, ‘best’]

Using for loops

You can easily remove the empty string from a list using loops it’s a simple technique. There is no issue to use any kind of loops in Python. While or for loop? Which one you feel better.

Let’s see how you can create a Python script:

  • First of all, make a list, and assign names with a few of them that should be empty.
  • Then use the while loop and in the last just use print() function.

Let’s have a quick code here.


wholeblogs = ["one", "" , "Two", "three", "four", "five", "", "six","", "", "seven", "eight", "", "" ,"Nine", "ten"]
 
for i in wholeblogs:
	if "" in wholeblogs:
		wholeblogs.remove("")

print(wholeblogs)

Output:

[‘one’, ‘Two’, ‘three’, ‘four’, ‘five’, ‘six’, ‘seven’, ‘eight’, ‘Nine’, ‘ten’]

Using filter()

If you want fast service then we & many peoples are recommending it with highly. Because it is too fast. It is the most pretty easy to remove empty.

Wholeblogs = ["A", " " , "B", "C", "D", "E", " ", "F"," ", "G", " ", "H", " ", "I" ,".....", "Z"]

Wholeblogs = list(filter(lambda string : string.strip(), Wholeblogs))

print(Wholeblogs)

Output:

[‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘…..’, ‘Z’]

Using list comprehension

Here In List comprehension, you can also remove empty strings from a list of strings let’s see the process of list comprehension.

Wholeblogs = ["", "Wholeblogs", "", "is", "great platform", ""]

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

# using list comprehension to
# perform removal
Wholeblogs = [i for i in Wholeblogs if i]

# Printing modified list
print ("Modified list is : " + str(Wholeblogs))

Output:

Original list is : [”, ‘Wholeblogs’, ”, ‘is’, ‘great platform’, ”]

Modified list is : [‘Wholeblogs’, ‘is’, ‘great platform’]

join() split()

You can also be achieved this task by join() & split() operations in Python.

The join() first of all combines all strings so that empty space is removed.

Wholeblogs = ["", "Wholeblogs", "", "is", "great platform", ""]

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

# using join() + split() to
# perform removal
Wholeblogs = ' '.join(Wholeblogs).split()

# Printing modified list
print ("Modified list is : " + str(Wholeblogs))

Output:

Original list is : [”, ‘Wholeblogs’, ”, ‘is’, ‘great platform’, ”]

Modified list is : [‘Wholeblogs’, ‘is’, ‘great’, ‘platform’]

LEAVE A REPLY

Please enter your comment!
Please enter your name here