19-Jul-2021
.
Admin
Hello Friends,
In this blog, I will show you how to remove empty list from list using python. We will talk about remove empty list from list in python.
This article will give you example for remove empty list in list using python. first example in using filter() to remove empty list and another example in you can remove all empty lists from a list of lists by using the list comprehension statement [x for x in list if x != []] to filter the 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 = [1, [], 2, 3, [], 4, 5, [], [], 9]
# 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 : [1, [], 2, 3, [], 4, 5, [], [], 9]
List after empty list removal : [1, 2, 3, 4, 5, 9]
Example 2
# Python Remove empty List from List
# using list comprehension
# Initializing list
myList = [1, [], 2, 3, [], 4, 5, [], [], 9]
# 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 : [1, [], 2, 3, [], 4, 5, [], [], 9]
List after empty list removal : [1, 2, 3, 4, 5, 9]
It will help you....
#Python