Python – Remove empty List from List Example
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....
- Get Last 2 Digits of Number in Python Tutorial Example
- How to Sum of First and Last Digit of Number in Python?
- How to Get First and Last Digit of Number in Python?
- Get the Last Digit of Number in Python Tutorial Example
- Get the First Digit of Number in Python Tutorial Example
- How to Remove All Decimals from Number in Python?
- How to Convert String to Float with 2 Decimal Places in Python?
- How to Format Number to 2 Decimal Places in Python?