Python – Remove empty List from List
Sometimes, while working with python, 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 applications in many domains. Let us discuss certain ways in which the removal of empty lists can be performed.
Method 1: Using list comprehension: This is one of the ways in which this problem can be solved. In this, we iterate through the list and don’t include the list which is empty.
Example
Python3
# Python3 code to Demonstrate Remove empty List# from List using list comprehension# Initializing listtest_list = [5, 6, [], 3, [], [], 9]# printing original listprint("The original list is : " + str(test_list))# Remove empty List from List# using list comprehensionres = [ele for ele in test_list if ele != []]# printing resultprint("List after empty list removal : " + str(res)) |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using filter() method
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.
Example
Python3
# Python3 Code to Demonstrate Remove empty List# from List using filter() Method# Initializing list by custom valuestest_list = [5, 6, [], 3, [], [], 9]# Printing original listprint("The original list is : " + str(test_list))# Removing empty List from List# using filter() methodres = list(filter(None, test_list))# Printing the resultant listprint("List after empty list removal : " + str(res)) |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3: Using function definition
Python3
# Python Code to Remove empty List from Listdef empty_list_remove(input_list): new_list = [] for ele in input_list: if ele: new_list.append(ele) return new_list# input list valuesinput_list = [5, 6, [], 3, [], [], 9]# print initial list valuesprint(f"The original list is : {input_list}")# function-call & print valuesprint(f"List after empty list removal : {empty_list_remove(input_list)}") |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Above defined method(Method 3) is best optimized method among all three.
Method 4: Using len() and type() methods.If the length is zero then the list is empty.
Python3
# Python3 code to Demonstrate Remove empty List# Initializing listtest_list = [5, 6, [], 3, [], [], 9]# printing original listprint("The original list is : " + str(test_list))new_list=[]# Remove empty List from Listfor i in test_list: x=str(type(i)) if(x.find('list')!=-1): if(len(i)!=0): new_list.append(i) else: new_list.append(i)# printing resultprint("List after empty list removal : " + str(new_list)) |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Method 5: Using remove() method
Python3
# Python3 code to Demonstrate Remove empty List# Initializing listtest_list = [5, 6, [], 3, [], [], 9]# printing original listprint("The original list is : " + str(test_list))# Remove empty List from Listwhile [] in test_list : test_list.remove([])# printing resultprint("List after empty list removal : " + str(test_list)) |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Time Complexity: O(n), where n is length of list
Auxiliary Space: O(n)
Method 6 : Using list(),map(),join() and replace() methods
Python3
# Python3 code to Demonstrate Remove empty List# Initializing listtest_list = [5, 6, [], 3, [], [], 9]# printing original listprint("The original list is : " + str(test_list))x=list(map(str,test_list))y="".join(x)y=y.replace("[]","")y=list(map(int,y))# printing resultprint("List after empty list removal : " + str(y)) |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Method: Using enumerate function
Python3
test_list = [5, 6, [], 3, [], [], 9]res = [ele for i,ele in enumerate(test_list) if ele != []]print(res) |
[5, 6, 3, 9]
Method: Using filter function
Python
# Python3 code to Demonstrate Remove empty List# Initializing listtest_list = [5, 6, [], 3, [], [], 9]# printing original listprint("The original list is : " + str(test_list))# Remove empty List from Listres = filter(None, test_list)# printing resultprint("List after empty list removal : " ,res) |
The original list is : [5, 6, [], 3, [], [], 9]
('List after empty list removal : ', [5, 6, 3, 9])Method: Using lambda function
Python3
# Python3 Code to Demonstrate Remove empty List# from List using lambda function# Initializing list by custom valuestest_list = [5, 6, [], 3, [], [], 9]# Printing original listprint("The original list is : " + str(test_list))# Removing empty List from List# using lambda functionres = list(filter(lambda x: x != [], test_list))# Printing the resultant listprint("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: Using Recursion method
Python3
# Python3 code to Demonstrate Remove empty List#defining recursive function to remove empty listdef remove_empty(start,oldlist,newlist): if start==len(oldlist): #base condition return newlist if oldlist[start]==[]: #checking the element is empty list or not pass else: newlist.append(oldlist[start]) #appending non empty list element to newlist return remove_empty(start+1,oldlist,newlist) #recursive function calltest_list = [5, 6, [], 3, [], [], 9]# printing original listprint("The original list is : " + str(test_list))result=remove_empty(0,test_list,[])# printing resultprint("List after empty list removal : " ,result) |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Method: Using itertools.filterfalse()
Python3
# Python3 Code to Demonstrate Remove empty List# from Listimport itertools# Initializing list by custom valuestest_list = [5, 6, [], 3, [], [], 9]# Printing original listprint("The original list is : " + str(test_list))# Removing empty List from List# using lambda functionres = list(itertools.filterfalse(lambda x: x == [], test_list))# Printing the resultant listprint("List after empty list removal : " + str(res)) |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method: Using the map() function
Use the map function to iterate through the original list and remove empty lists.
Python3
# Initializing listtest_list = [5, 6, [], 3, [], [], 9]# printing original listprint("The original list is : " + str(test_list))# Remove empty List from List# using map() functionres = list(map(lambda x: x if x != [] else None, test_list))res = [x for x in res if x != None]# printing resultprint("List after empty list removal : " + str(res))#This code is contributed by Vinay Pinjala. |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Time Complexity: O(N)
Auxiliary Space: O(N)
Using re module
Python3
import re # input list valuesinput_list = [5, 6, [], 3, [], [], 9] # print initial list valuesprint(f"The original list is : {input_list}") # removing empty list from listres = list(filter(None, [x for x in input_list if not re.match('\[\]', str(x))])) # print resultant listprint(f"List after empty list removal : {res}") |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method: Using the pop()
Python3
# Define the test listtest_list = [5, 6, [], 3, [], [], 9]# Print the original listprint("The original list is : " + str(test_list))# Counter variable 'i' to keep track of the current indexi = 0# While loop to go through all elements of the listwhile i < len(test_list): # If the current element is an empty list, remove it from the list if test_list[i] == []: test_list.pop(i) # Else, increment the counter variable else: i += 1# Reassign the result to the original list after removing all empty listsres = test_list# Print the resultprint("List after empty list removal : " + str(res))#This code is contributed by Jyothi pinjala. |
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : [5, 6, 3, 9]
Time Complexity: O(N)
Auxiliary Space: O(1)



Please Login to comment...