THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial shows remove empty string space from a list in python with code examples
For example, Given list is [“one”,“two”,“three”,"",“four”], the output is [“one”,“two”,“three”,“four”]
There are multiple ways we can delete an empty string from a list.
filter
method iterates the list and accepts None and list, returns the iterator to return list.
It is easy and simple, and faster.names=["one","two","three","","four"]
result = filter(None, names)
print(list(result));
It returns a new list from following
Here is an example
result1=[name for name in names if name]
print(result1)
Here is an example
results2=filter(lambda item: len(item)>0, names)
print(list(results2))
Learned multiple ways to remove the empty string from a list.
🧮 Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts