How to Remove empty List from List in Python ?
Published On: 22/06/2022 | Category:
Python
Hi Dev,
This post will give you example of python remove empty list from list. you can understand a concept of remove empty list from nested list python. step by step explain how to remove empty lists in python. you'll learn remove empty list from list python. You just need to some step to done python - remove empty list from list.
Here i will give you two example for python remove empty list from list example. So let's see the bellow example:
Example 1 : Using filter()#Python Remove empty List from List using filter() # Initializing list myList = [2, [], 5, 8, [], 4, 3, [], [], 1] # printing original list print("The Original List is : " + str(myList)) # Remove empty List from List result = list(filter(None, myList)) # printing result print ("List After Empty List Removal : " + str(result))Output:
The Original List is : [2, [], 5, 8, [], 4, 3, [], [], 1] List After Empty List Removal : [2, 5, 8, 4, 3, 1]Example : 2
# Python Remove empty List from List # using list comprehension # Initializing list myList = [2, [], 5, 8, [], 4, 3, [], [], 1] # printing original list print("The Original List is : " + str(myList)) # Remove empty List from List result = [ele for ele in myList if ele != []] print ("List After Empty List Removal : " + str(result))Output:
The Original List is : [2, [], 5, 8, [], 4, 3, [], [], 1] List After Empty List Removal : [2, 5, 8, 4, 3, 1]
I Hope It will help you....