Open In App

Python – Remove empty List from List

Last Updated : 13 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 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]

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 values
test_list = [5, 6, [], 3, [], [], 9]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Removing empty List from List
# using filter() method
res = list(filter(None, test_list))
 
# Printing the resultant 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




# Python Code to Remove empty List from List
 
def empty_list_remove(input_list):
    new_list = []
    for ele in input_list:
        if ele:
            new_list.append(ele)
    return new_list
 
 
# input list values
input_list = [5, 6, [], 3, [], [], 9]
 
# print initial list values
print(f"The original list is : {input_list}")
# function-call & print values
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




# Python3 code to Demonstrate Remove empty List
 
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
 
# printing original list
print("The original list is : " + str(test_list))
new_list=[]
# Remove empty List from 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)
# printing result
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




# Python3 code to Demonstrate Remove empty List
 
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove empty List from List
while [] in test_list :
    test_list.remove([])
 
# printing result
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




# Python3 code to Demonstrate Remove empty List
 
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
 
# printing original list
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))
 
# printing result
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)


Output

[5, 6, 3, 9]

Method: Using filter function

Python




# Python3 code to Demonstrate Remove empty List
 
# Initializing list
test_list = [5, 6, [], 3, [], [], 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove empty List from List
res = filter(None, test_list)
 
# printing result
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




# Python3 Code to Demonstrate Remove empty List
# from List using lambda function
 
# Initializing list by custom values
test_list = [5, 6, [], 3, [], [], 9]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Removing empty List from List
# using lambda function
res = list(filter(lambda x: x != [], test_list))
 
# Printing the resultant 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




# Python3 code to Demonstrate Remove empty List
#defining recursive function to remove empty list
def 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 call
 
test_list = [5, 6, [], 3, [], [], 9]
 
# printing original list
print("The original list is : " + str(test_list))
result=remove_empty(0,test_list,[])
# printing result
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




# Python3 Code to Demonstrate Remove empty List
# from List
import itertools
 
# Initializing list by custom values
test_list = [5, 6, [], 3, [], [], 9]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Removing empty List from List
# using lambda function
res = list(itertools.filterfalse(lambda x: x == [], test_list))
 
# Printing the resultant 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




# 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 map() function
res = list(map(lambda x: x if x != [] else None, test_list))
res = [x for x in res if x != None]
 
# printing result
print("List after empty list removal : " + str(res))
#This code is contributed by Vinay Pinjala.


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 values
input_list = [5, 6, [], 3, [], [], 9]
  
# print initial list values
print(f"The original list is : {input_list}")
  
# removing empty list from list
res = list(filter(None, [x for x in input_list if not re.match('\[\]', str(x))]))
  
# print resultant list
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




# Define the test list
test_list = [5, 6, [], 3, [], [], 9]
# Print the original list
print("The original list is : " + str(test_list))
# Counter variable 'i' to keep track of the current index
i = 0
# While loop to go through all elements of the list
while 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 lists
res = test_list
# Print the result
print("List after empty list removal : " + str(res))
 
 
#This code is contributed by Jyothi pinjala.


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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads