Remove empty lists from list Python

There may be a chances of having a python list containing empty lists, if you would like to remove all of them this guide helps you.

Remove empty lists from a Python List:

There are two different ways to do it, lets see both of them.

1. Python filter()

Python built-in filter() function is used to filer the elements in a list, we can use this function to filter the empty lists.

if __name__ == '__main__': fruits_list = [[], 'apple',[], [], [], [], 'grapes', [], 'banana'] print("Before filtering") print(fruits_list) print("After filtering") fruits_list = list(filter(None,fruits_list)) print(fruits_list)

Output:

Before filtering [[], 'apple', [], [], [], [], 'grapes', [], 'banana'] After filtering ['apple', 'grapes', 'banana']

2. List Comprehension:

The same thing we can do with python’s list comprehension like below.

if __name__ == '__main__': fruits_list = [[], 'apple',[], [], [], [], 'grapes', [], 'banana'] print("Before filtering") print(fruits_list) print("After filtering with list comprehension") fruits_list = [fruit for fruit in fruits_list if fruit != []] print(fruits_list)

Output:

Before filtering [[], 'apple', [], [], [], [], 'grapes', [], 'banana'] After filtering with list comprehension ['apple', 'grapes', 'banana']

References:

  • Python filter
  • Python List in depth

Happy Learning 🙂

FacebookTwitterRedditLinkedInTumblrPinterestVkEmail

Python

Nicesnippets

19-07-2021

Hello Friends,

In this blog, I will show you how to remove empty list from list using python. We will talk about remove empty list from list in python.

This article will give you example for remove empty list in list using python. first example in using filter() to remove empty list and another example in you can remove all empty lists from a list of lists by using the list comprehension statement [x for x in list if x != []] to filter the list.

Here i will give you two example for python remove empty list from list example. So let's see the bellow example:

Example 1 : Using filter()

#Python Remove empty List from List using filter()

# Initializing list

myList = [1, [], 2, 3, [], 4, 5, [], [], 9]

# printing original list

print("The original list is : " + str(myList))

# Remove empty List from List

result = list(filter(None, myList))

# printing result

print ("List after empty list removal : " + str(result))

Output:

The original list is : [1, [], 2, 3, [], 4, 5, [], [], 9]

List after empty list removal : [1, 2, 3, 4, 5, 9]

Example 2

# Python Remove empty List from List

# using list comprehension

# Initializing list

myList = [1, [], 2, 3, [], 4, 5, [], [], 9]

# printing original list

print("The original list is : " + str(myList))

# Remove empty List from List

result = [ele for ele in myList if ele != []]

print ("List after empty list removal : " + str(result))

Output:

The original list is : [1, [], 2, 3, [], 4, 5, [], [], 9]

List after empty list removal : [1, 2, 3, 4, 5, 9]

It will help you....

The easiest way is list comprehension to remove empty elements from a list in Python. And another way is to use the filter() method. The empty string "" contains no characters and empty elements could be None or [ ], etc.

Python remove empty elements from a list Example

Simple examples code.

Using list comprehension

Simp;e iterate through the list and add the non-empty elements.

list1 = ['A', ' ', ' ', 'B', ' ', 'C'] res = [ele for ele in list1 if ele.strip()] print(res) list2 = [1, 6, [], 3, [], [], 9] res = [ele for ele in list2 if ele != []] print(res)

Output:

Using filter() method

Just filter out the None and empty element form list.

If None is used as the first argument to filter(), it filters out every value in the given list, which is False in a boolean context. This includes empty lists.

list2 = [1, 6, [], 3, [], [], 9] res = list(filter(None, list2)) print(res)

Output: [1, 6, 3, 9]

Use a for loop to remove empty strings from a list

Iterate through the list and check if each string is not an empty string. If it is not an empty string, then add each non-empty string to an initially empty list using the append method.

list1 = ['A', ' ', ' ', 'B', ' ', 'C'] res = [] for i in list1: if i.strip() != '': res.append(i) print(res) list2 = [1, 6, [], 3, [], [], 9] res = [] for i in list2: if i: res.append(i) print(res)

Output:

[‘A’, ‘B’, ‘C’]
[1, 6, 3, 9]

If you want to get rid of everything that is “falsy”, e.g. empty strings, empty tuples, zeros, you could also use

list2 = [x for x in list1 if x]

Do comment if you have any doubts and suggestions on this Python List topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

a = [[1,'aa',3,12,'a','b','c','s'],[],[],[1,'aa',7,80,'d','g','f',''],[9,None,11,12,13,14,15,'k']] b=[] for lng in range(len(a)): if(len(a[lng])>=1):b.append(a[lng]) a=b print(a)

Output:

[[1,'aa',3,12,'a','b','c','s'],[1,'aa',7,80,'d','g','f',''],[9,None,11,12,13,14,15,'k']]

Sometimes, while working with python data, 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 application in many domains. Let’s discuss certain ways in which removal of empty lists can be performed.

Method #1 : Using list comprehension
This is one of the way in which this problem can be solved. In this, we iterate through the list and don’t include the list which is empty.

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]

Method #2 : Using filter()
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.

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]


Article Tags :

Video liên quan

Chủ đề