There are several ways to remove empty strings from strings :
def remove_empty_strings(strings): result = [] for string in strings: if string != "": result.append(string) return result
def remove_empty_strings(strings): return [string for string in strings if string != ""]
def remove_empty_strings(strings): return list(filter(lambda string: string != "", strings))
You can remove empty strings from strings by calling these functions, for example:
strings = ["hello", "", "world", "", "python"] result = remove_empty_strings(strings) print(result)
Output:
['hello', 'world', 'Python']
The above is the detailed content of How to remove empty strings in python. For more information, please follow other related articles on the PHP Chinese website!