Open In App

Remove Empty Lists from List - Python

Last Updated : 28 Oct, 2025
Comments
Improve
Suggest changes
31 Likes
Like
Report

Given a list containing multiple sublists, the task is to remove all empty sublists from it. Removing empty lists means keeping only the sublists that have at least one element.

For example:

a = [[1, 2], [], [3, 4], [], [5]]
Resulting list = [[1, 2], [3, 4], [5]]

Let’s explore different methods to remove empty lists from a list.

Using list comprehension

This method uses list comprehension and creates a new list by including only the non-empty sublists from the original list.

Python
a = [[1, 2], [], [3, 4], [], [5]]
res = [b for b in a if b]
print(res)

Output
[[1, 2], [3, 4], [5]]

Explanation: [b for b in a if b] iterates over each sublist in a. Only sublists that are non-empty (if b) are included in the new list res.

Using filter() with lambda

It filters out empty sublists using the filter() function along with a lambda function.

Python
a = [[1, 2], [], [3, 4], [], [5]]
res = list(filter(lambda b: b, a))
print(res)

Output
[[1, 2], [3, 4], [5]]

Explanation:

  • lambda b: b returns True for non-empty sublists and False for empty ones.
  • filter() keeps only sublists where the lambda returns True.
  • list() converts the filtered result back to a list.

Using NumPy

For lists containing numeric data, NumPy can efficiently remove empty sublists.

Python
import numpy as np
a = [[1, 2], [], [3, 4], [], [5]]
arr = np.array(a, dtype=object)
res = arr[[len(x) > 0 for x in arr]].tolist()
print(res)

Output
[[1, 2], [3, 4], [5]]

Explanation:

  • [len(x) > 0 for x in arr] creates a boolean mask: True for non-empty sublists, False for empty ones.
  • arr[mask] selects only the non-empty sublists.
  • .tolist() converts the filtered NumPy array back to a regular Python list.

Using for loop

In this method, Iterate through the list and check each item if it is empty or not. If the list is not empty then add it to the result list.

Python
a = [[1, 2], [], [3, 4], [], [5]]
res = []
for b in a:
    if b:
        res.append(b)
print(res)

Output
[[1, 2], [3, 4], [5]]

Explanation:

  • Iterate over each sublist in a.
  • Append it to res only if it is non-empty (if b).

Explore