![Python-courses-python-exercises-python--Python-courses-configure script-run-script](https://1.bp.blogspot.com/-T3BongcoJFo/X4CftQxVL8I/AAAAAAAAB5U/Lf59WgNyhGo2FdogKqxv0PIy_Ef7XDPdgCLcBGAsYHQ/s0/my-python-courses-python-exercises-with-solutions.png)
Exercise 82
Write a Python algorithm that remove all empty strings from list of strings Example:
If :
L = ["Python" , "" , "is" , "" , "the", "most" , "", "used" , "programming", "language" , ""]
The output is:
['Python', 'is', 'the', 'most', 'used', 'programming', 'language']
Solution
def remove_empty_str(L):
# initializing the list with no string
newList = []
# iterate over all element of the list L
for word in L:
if word != "":
newList.append(word)
return newList
# Example
L = ["Python" , "" , "is" , "" , "the", "most" , "", "used" , "programming", "language" , ""]
print(remove_empty_str(L))
# Expected output is :
# ['Python', 'is', 'the', 'most', 'used', 'programming', 'language']
Younes Derfoufi
my-courses.net
my-courses.net