A Python list is a sequential collection of data objects, and these data objects can be homogeneous or heterogeneous. In this Python program, you will learn about different Python tricks to know how to remove an empty string from a list of strings in Python.
An empty string is also a data object, although it represents a False value in Python. Still, it is a data object that stores some space in the memory. And when we use an empty string inside a Python list like other data values, a Python list associates an index value to a Python list.
Many times in data analysis, the empty values like an empty string are just noises that need to be removed from the data structure like a list.
Now let's see the different techniques you can use to remove all the empty strings from a list of strings or any list.
How to Remove an Empty String From a List of Strings in Python?
1) Using Loops
Removing an empty string from a list using a loop is a pretty straightforward technique. You can also call it a Brute-force technique for removing all the empty strings. To remove all the empty strings, we can either use while or for loop.
Let's see how we can write a Python script using a loop that can remove all the empty strings from a list.
i) Using the While Loop
str_list = ["one", "" , "Two", "three", "four", "five", "", "six","", "", "seven", "eight", "", "" ,"Nine", "ten"]
while "" in str_list:
str_list.remove("")
print(str_list)
Output
['one', 'Two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'Nine', 'ten']
ii) Using the For Loop
str_list = ["one", "" , "Two", "three", "four", "five", "", "six","", "", "seven", "eight", "", "" ,"Nine", "ten"]
for _ in str_list:
if "" in str_list:
str_list.remove("")
print(str_list)
Output
['one', 'Two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'Nine', 'ten']
The above two programs will remove the empty strings from the list, but they are not that efficient because if there is an empty string with multiple spaces, then the program will not be able to remove those empty strings.
In that case, we can either use the Python filter function or list comprehension. Although
" "
is not an empty string because <space> is also a valid character, but for this tutorial, we will also be considering only spaces string as an empty string and removing them from the list.
2) Using filter()
Using the filter() function , we can remove all the empty strings with single or multiple spaces efficiently.
Example 1
str_list = ["one", " " , "Two", "three", "four", "five", " ", "six"," ", " ", "seven", "eight", " ", " " ,"Nine", "ten"]
str_list = list(filter(lambda string : string.strip(), str_list))
print(str_list)
Output
['one', 'Two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'Nine', 'ten']
Behind the Code
The
strip()
function will remove all the empty spaces, and the
lambda
function will return the complete string value or empty value. The filter function will only accept the string value, and for the empty string value, it will treat them as false and filter them out.
3) Using List comprehension
We can also use Python list comprehension to remove the empty strings from the list.
Example
str_list = ["one", " " , "Two", "three", "four", "five", " ", "six"," ", " ", "seven", "eight", " ", " " ,"Nine", "ten"]
str_list = [string for string in str_list if string.strip()]
print(str_list)
Output
['one', 'Two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'Nine', 'ten']
Compared to list comprehension, using the
filter()
function is better in performance while removing empty strings.
4) Using join() and split() Functions
We can also remove the empty strings from the list with the combination of
join()
and
split()
functions.
Example
str_list = ["one", " " , "Two", "three", "four", "five", " ", "six"," ", " ", "seven", "eight", " ", " " ,"Nine", "ten"]
str_list = " ".join(str_list).split()
print(str_list)
Output
['one', 'Two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'Nine', 'ten']
Behind the code
First, the
" ".join(str_list)
statement will join the list using
" "
single empty space and return a list
one Two three four five six seven eight Nine ten
. Then the split() method will convert every word of the string into a list by removing all the spaces.
Conclusion
In the Python tutorial, you learned how to remove an empty string from a list of strings in Python using four different ways. Here we have also treated only spaced string as an empty string, but in reality, a spaced string is not an empty string. Just for the sake of data analysis, we considered no data value string as an empty string.
People are also reading:
- Extract YouTube Comments in Python
- Edge Detection in Python
- How to Apply HOG Feature Extraction in Python?
- Automated Browser testing in Python
- Image Transformations in Python
- Python Dictionary Append
- Python Main Function and Method Example
- How to Use GitHub API in Python?
- Read Emails in Python
- MySQL Database in Python
Leave a Comment on this Post