Remove Empty Lists

Remove empty sublists from a list of lists.

PythonBeginner
Python
# Program to remove empty lists from a list

items = eval(input("Enter a list of lists (e.g., [[], [1], [], [2, 3]]): "))

filtered = [x for x in items if x]

print("After removing empty lists:", filtered)

Output

Enter a list of lists (e.g., [[], [1], [], [2, 3]]): [[], [1], [], [2, 3]]
After removing empty lists: [[1], [2, 3]]

An empty list is falsy, so we filter on truthiness to keep only non-empty lists.