Write a Python Program to Eliminate All Empty Lists From a Given List of Lists

Hi in this article let's learn a Python program to delete all the empty lists present in the given nested list.  I will try to solve this by  declaring a nested list inside the program and using the logic to eliminate all the empty lists from the given list.

I will be using  enumerate function to get the index

 

Write a Python Program to Eliminate All Empty Lists From a Given List of Lists

 



Write a Python Program to Eliminate All Empty Lists From a Given List of Lists

Take a variable named list1,  containing the list of numbers also having a nested list within it as shown below.  the nested list contains numbers,  3 nested list and two empty list inside it

list1 = [1, 2, [3, 4], 7, [], [8, 9, 9], [], [56]]





Use the following to enumerate all the items present in the list using the if statement check if if item is empty and is a instance of list then using the delete keyword delete the list containing the index item

for index, item in enumerate(list1):
   if (not item) and isinstance(item, list):
       del list1[index]





Finally once the for loop is completed, using the print function print all the items of a list.

print("List After Eliminating All Empty List is ...")
print(list1)





The above Python problem can also be solved using the empty list logic. Take an empty list and compare it with the item of the  input list.  if the item is an Empty then delete the list item as shown below



list1 = [1, 2, [3, 4], 7, [], [8, 9, 9], [], [56]]

Inside the program let's take an empty list and assign it. Using the for loop, get the index and the item by  enumerating the above list. Check if the the list item is empty by comparing  the the item with the empty list if so delete the the list item containing the index

empty_list = []
for index, item in enumerate(list1):
   if item == empty_list and isinstance(item, list):
       del list1[index]
      
print("List After Eliminating All Empty List is ...")
print(list)





Complete python code #1
=============================

list1 = [1, 2, [3, 4], 7, [], [8, 9, 9], [], [56]]

for index, item in enumerate(list1):
   if (not item) and isinstance(item, list):
       del list1[index]

print("List After Eliminating All Empty List is ...")
print(list1)







Complete python code #2
===============================

list1 = [1, 2, [3, 4], 7, [], [8, 9, 9], [], [56]]

empty_list = []
for index, item in enumerate(list1):
   if item == empty_list and isinstance(item, list):
       del list1[index]
      
print("List After Eliminating All Empty List is ...")
print(list)






Output : Both the code provided above will result in the same output
=======================================================


List After Eliminating All Empty List is ...
[1, 2, [3, 4], 7, [8, 9, 9], [56]]




Conclusion
=================
Execute the above Python program by changing the input list declared inside the program and note down the results.


Comment it down below if you have any suggestions to improve the above Python program

Post a Comment

0 Comments