Python Program to Remove Empty Strings from a List
In this article, we will be solving question based on strings and list in Python. According to the question we have to remove empty strings from a list.
If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.
Question - Write a Python Program to Remove Empty Strings from a List
Logic and Explanation
Question - Write a Python Program to Remove Empty Strings from a List
Python Program to Remove Empty Strings from a List - In this program we have to remove empty strings from a list.
For this we can use the remove method of List. Remove is a built-in list method which removes a particular element of a list which we give it as a parameter. So, we will pass the empty string in this method and it will remove the empty string from the list.
BUT it only removes the first occurrence of the element in the list and if there will be more than one empty string in the list then we have to run it multiple times until all the empty string in the list are removed.
So, I hope this part is clear to you. Now, let's move towards writing the code. Before that let's write the pseudo code of the program.
- First check whether the given string contains an empty string or not.
- If it contains an empty string, then call remove("") method.
- Again, check for the empty string till all the empty strings from the list are removed.
L = ["Python","Coding","","Hello","","World"]
while "" in L:
L.remove("")
print(L)
Output
['Python', 'Coding', 'Hello', 'World']
So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.
For More Such Python Programs - Click Here
Enjoy Coding!!!
Also Read :-
Extract Phone Number Details Using Python
thats good
ReplyDeletePost a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.