How to use 'python remove empty strings from list' in Python

Every line of 'python remove empty strings from list' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
67def clean_lines(string_list, remove_empty_lines=True):
68 """
69 Strips whitespace, \n and \r and empty lines from a list.
70
71 Args:
72 string_list:
73 List of strings
74 remove_empty_lines:
75 Set to True to skip lines which are empty after stripping.
76
77 Returns:
78 List of clean strings with no whitespaces.
79 """
80
81 for s in string_list:
82 clean_s = s
83 if '#' in s:
84 ind = s.index('#')
85 clean_s = s[:ind]
86 clean_s = clean_s.strip()
87 if (not remove_empty_lines) or clean_s != '':
88 yield clean_s
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

158def remove_newlines(value):
159 if not value:
160 return ''
161 elif isinstance(value, six.string_types):
162 return value.replace('\r', '').replace('\n', '')
163 else:
164 return value

Related snippets