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)) |
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)) |
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.
Recommended Posts:
- Python | Remove empty tuples from a list
- Python | Remove and print every third from list until it becomes empty
- Python | Remove empty strings from list of strings
- Python | Remove trailing empty elements from given list
- Python - Ways to remove multiple empty spaces from string List
- Python - Remove empty value types in dictionaries list
- Python - Check if a list is empty or not
- Python | First Non-Empty String in list
- Python | Splitting list on empty string
- Python | Initialize list with empty dictionaries
- Python | Check if any String is empty in list
- Python: Get List of all empty Directories
- Declare an empty List in Python
- Python - Test for Empty Dictionary Value List
- Python | Remove all values from a list present in other list
- How to write an empty function in Python - pass statement?
- numpy.empty() in Python
- Python program to check if string is empty or not
- Python | Initializing dictionary with empty lists
- Python | Pandas TimedeltaIndex.empty
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

