Use the built-in function filter() to remove empty strings from a list
Or use the for loop and if condition to remove the empty strings from a list
Solution1:Using the loop and if condition
str_list = ["Emma", "Jon", "", "Kelly", None, "Eric", ""]
res_list = []
for s in str_list:
# check for non empty string
if s:
res_list.append(s)
print(res_list)
Solution2:Using the built-in function filter()
str_list = ["Emma", "Jon", "", "Kelly", None, "Eric", ""]
# use built-in function filter to filter empty value
new_str_list = list(filter(None, str_list))
print("After removing empty strings")
print(new_str_list)
Hint:
filter()
to remove empty strings from a listif
condition to remove the empty strings from a listSolution1:Using the loop and
if
conditionSolution2:Using the built-in function
need an explanation for this answer? contact us directly to get an explanation for this answerfilter()