Exercise 82

Create a Python algorithm that eliminates all empty strings from a given list of strings.
For instance, given the input list:

L = ["Python", "", "is", "", "the", "most", "", "used", "programming", "language", ""]

The algorithm should produce the following output:

['Python', 'is', 'the', 'most', 'used', 'programming', 'language']

You can use the list comprehension to iterate through each element in the input list L and includes it in the resulting list only if it is not an empty string.

Solution

# Input list
L = ["Python", "", "is", "", "the", "most", "", "used", "programming", "language", ""]

# Remove empty strings using list comprehension
result = [s for s in L if s != ""]

# Print the result
print(result)
"""
This will output:
['Python', 'is', 'the', 'most', 'used', 'programming', 'language']
"""




 

Younes Derfoufi
my-courses.net

One thought on “Solution Exercise 82 : Removing Empty Strings from a List”

Leave a Reply