Python – Remove empty List from List
Last Updated :
13 Mar, 2023
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
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
res = [ele for ele in test_list if ele ! = []]
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]
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
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( None , test_list))
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]
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3: Using function definition
Python3
def empty_list_remove(input_list):
new_list = []
for ele in input_list:
if ele:
new_list.append(ele)
return new_list
input_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print (f "The original list is : {input_list}" )
print (f "List after empty list removal : {empty_list_remove(input_list)}" )
|
Output
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
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
new_list = []
for 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)
print ( "List after empty list removal : " + str (new_list))
|
Output
The original list is : [5, 6, [], 3, [], [], 9]
List after empty list removal : [5, 6, 3, 9]
Method 5: Using remove() method
Python3
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
while [] in test_list :
test_list.remove([])
print ( "List after empty list removal : " + str (test_list))
|
Output
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
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
x = list ( map ( str ,test_list))
y = "".join(x)
y = y.replace( "[]" ,"")
y = list ( map ( int ,y))
print ( "List after empty list removal : " + str (y))
|
Output
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)
|
Method: Using filter function
Python
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
res = filter ( None , test_list)
print ( "List after empty list removal : " ,res)
|
Output
The original list is : [5, 6, [], 3, [], [], 9]
('List after empty list removal : ', [5, 6, 3, 9])
Method: Using lambda function
Python3
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: x ! = [], test_list))
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: Using Recursion method
Python3
def remove_empty(start,oldlist,newlist):
if start = = len (oldlist):
return newlist
if oldlist[start] = = []:
pass
else :
newlist.append(oldlist[start])
return remove_empty(start + 1 ,oldlist,newlist)
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
result = remove_empty( 0 ,test_list,[])
print ( "List after empty list removal : " ,result)
|
Output
The original list is : [5, 6, [], 3, [], [], 9]
List after empty list removal : [5, 6, 3, 9]
Method: Using itertools.filterfalse()
Python3
import itertools
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
res = list (itertools.filterfalse( lambda x: x = = [], test_list))
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]
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
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
res = list ( map ( lambda x: x if x ! = [] else None , test_list))
res = [x for x in res if x ! = None ]
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]
Time Complexity: O(N)
Auxiliary Space: O(N)
Using re module
Python3
import re
input_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print (f "The original list is : {input_list}" )
res = list ( filter ( None , [x for x in input_list if not re.match( '\[\]' , str (x))]))
print (f "List after empty list removal : {res}" )
|
Output
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
test_list = [ 5 , 6 , [], 3 , [], [], 9 ]
print ( "The original list is : " + str (test_list))
i = 0
while i < len (test_list):
if test_list[i] = = []:
test_list.pop(i)
else :
i + = 1
res = test_list
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]
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...