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]
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]
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]
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) |
Output:
The original list is : [5, 6, [], 3, [], [], 9] List after empty list removal : 5 6 3 9


