python - List Comprehension: Elegantly strip and remove empty ...
stackoverflow.com › questions › 28534125Feb 16, 2015 · It's seems that it is checking for empty elements, THEN stripping and re-inserting elements into the list. Thank you in advance! # input char_list = ['', ' a','b', '\t'] print char_list char_list = [x.strip() for x in char_list if x!=''] print char_list # output ['', ' a', 'b', '\t'] ['a', 'b', ''] #DESIRED output ['', ' a', 'b', '\t'] ['a', 'b']
Strip a list in Python - Stack Overflow
stackoverflow.com › questions › 41494191Jan 11, 2017 · Else, may convert the list to str and remove the content from the string based on the list of items you want to remove. For example: For example: my_list = ['01', 1.0, [0.2]] remove_content = ["'", "[", "]"] # Content you want to be removed from `str` my_str = repr(my_list) # convert list to `str` for content in remove_content: my_str = my_str.replace(content, '')