Python-courses-python-exercises-python--Python-courses-configure script-run-script

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

Leave a Reply