Avatar ·

Removing empty elements from a list python

📁 вопрос

Sorry for the stupid question, but how to remove empty elements from a list in python?

Avatar ·

Don't name variables after built-in types!

You can use regular expressions by replacing the first space with nothing and keeping the second

import reregex = r""\\s(\\s)?""test_str = ""'A l a  m a  k o t a'""subst = ""\\\\1""print(re.sub(regex, subst, test_str)) # 'Ala ma kota'


and this way you can replace any number of spaces, more than one, with a single space

regex = r""\\s((\\s)(\\s+)?)?""subst = ""\\\\2""result = re.sub(regex, subst, test_str)

Log in to leave an answer

Blogs