Skip to content
Related Articles

Related Articles

Python – Remove empty List from List

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 29 Sep, 2022
View Discussion
Improve Article
Save Article

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]

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]

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]

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!