Skip to content
Related Articles

Related Articles

Improve Article
Python – Remove empty List from List
  • Difficulty Level : Basic
  • Last Updated : 06 Mar, 2020

Sometimes, while working with python data, we can have a problem in which we need to filter out certain empty data. These can be None, empty string etc. This can have application in many domains. Let’s discuss certain ways in which removal of empty lists can be performed.

Method #1 : Using list comprehension
This is one of the way in which this problem can be solved. In this, we iterate through the list and don’t include the list which is empty.




# Python3 code to demonstrate 
# Remove empty List from List
# using list comprehension
  
# Initializing list 
test_list = [5, 6, [], 3, [], [], 9]
  
# printing original list 
print("The original list is : " + str(test_list))
  
# Remove empty List from List
# using list comprehension
res = [ele for ele in test_list if ele != []]
  
# printing result 
print ("List after empty list removal : " + str(res))
Output :
The original list is : [5, 6, [], 3, [], [], 9]
List after empty list removal : [5, 6, 3, 9]

 

Method #2 : Using filter()
This is yet another way in which this task can be performed. In this we filter None values. The none values include empty lists as well and hence these get removed.






# Python3 code to demonstrate 
# Remove empty List from List
# using filter()
  
# Initializing list 
test_list = [5, 6, [], 3, [], [], 9]
  
# printing original list 
print("The original list is : " + str(test_list))
  
# Remove empty List from List
# using filter
res = list(filter(None, test_list))
  
# printing result 
print ("List after empty list removal : " + str(res))
Output :
The original list is : [5, 6, [], 3, [], [], 9]
List after empty list removal : [5, 6, 3, 9]

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :