I have a list in the form
lst = ['', 'how', '', 'are', 'you', '']
and want to convert it into
lst = ['how','are', 'you']
I have currently used the code list(filter(None,lst)) but no changes are returned. How can i remove the empty strings?
I have a list in the form
lst = ['', 'how', '', 'are', 'you', '']
and want to convert it into
lst = ['how','are', 'you']
I have currently used the code list(filter(None,lst)) but no changes are returned. How can i remove the empty strings?
Your code must works but an other solution is using lambdas:
lst = list(filter(lambda x: x != '', lst))
print(lst)
Output: ['how', 'are', 'you']
A simple and possible solution is the following:
lst = ['', 'how', '', 'are', 'you', '']
lst = [element for element in lst if element != '']
Now, lst is ['how', 'are', 'you']
!= "" as it is not technically needed.Are you printing lst again? Your code works for me.
lst = ['', 'how', '', 'are', 'you', '']
lst = list(filter(None,lst))
print(lst)
outputs ['how', 'are', 'you']