There may be a chances of having a python list containing empty lists, if you would like to remove all of them this guide helps you. Show Remove empty lists from a Python List:There are two different ways to do it, lets see both of them. 1. Python filter()Python built-in filter() function is used to filer the elements in a list, we can use this function to filter the empty lists. if __name__ == '__main__': fruits_list = [[], 'apple',[], [], [], [], 'grapes', [], 'banana'] print("Before filtering") print(fruits_list) print("After filtering") fruits_list = list(filter(None,fruits_list)) print(fruits_list)Output: Before filtering [[], 'apple', [], [], [], [], 'grapes', [], 'banana'] After filtering ['apple', 'grapes', 'banana']2. List Comprehension:The same thing we can do with python’s list comprehension like below. if __name__ == '__main__': fruits_list = [[], 'apple',[], [], [], [], 'grapes', [], 'banana'] print("Before filtering") print(fruits_list) print("After filtering with list comprehension") fruits_list = [fruit for fruit in fruits_list if fruit != []] print(fruits_list)Output: Before filtering [[], 'apple', [], [], [], [], 'grapes', [], 'banana'] After filtering with list comprehension ['apple', 'grapes', 'banana']References:
Happy Learning 🙂
Python Nicesnippets 19-07-2021 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....
The easiest way is list comprehension to remove empty elements from a list in Python. And another way is to use the filter() method. The empty string "" contains no characters and empty elements could be None or [ ], etc. Python remove empty elements from a list ExampleSimple examples code. Using list comprehensionSimp;e iterate through the list and add the non-empty elements. list1 = ['A', ' ', ' ', 'B', ' ', 'C'] res = [ele for ele in list1 if ele.strip()] print(res) list2 = [1, 6, [], 3, [], [], 9] res = [ele for ele in list2 if ele != []] print(res)Output: Using filter() methodJust filter out the None and empty element form list. If None is used as the first argument to filter(), it filters out every value in the given list, which is False in a boolean context. This includes empty lists. list2 = [1, 6, [], 3, [], [], 9] res = list(filter(None, list2)) print(res)Output: [1, 6, 3, 9] Use a for loop to remove empty strings from a listIterate through the list and check if each string is not an empty string. If it is not an empty string, then add each non-empty string to an initially empty list using the append method. list1 = ['A', ' ', ' ', 'B', ' ', 'C'] res = [] for i in list1: if i.strip() != '': res.append(i) print(res) list2 = [1, 6, [], 3, [], [], 9] res = [] for i in list2: if i: res.append(i) print(res)Output: [‘A’, ‘B’, ‘C’] If you want to get rid of everything that is “falsy”, e.g. empty strings, empty tuples, zeros, you could also use list2 = [x for x in list1 if x]Do comment if you have any doubts and suggestions on this Python List topic.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical. a = [[1,'aa',3,12,'a','b','c','s'],[],[],[1,'aa',7,80,'d','g','f',''],[9,None,11,12,13,14,15,'k']] b=[] for lng in range(len(a)): if(len(a[lng])>=1):b.append(a[lng]) a=b print(a) Output: [[1,'aa',3,12,'a','b','c','s'],[1,'aa',7,80,'d','g','f',''],[9,None,11,12,13,14,15,'k']]
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
Output : The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9] Method #2 : Using filter()
Output : The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9] Article Tags : |