Remove Empty Lists
Remove empty sublists from a list of lists.
BeginnerTopic: List Programs
Python Remove Empty Lists Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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]]
Understanding Remove Empty Lists
An empty list is falsy, so we filter on truthiness to keep only non-empty lists.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.