You can use filter()
instead of a list comprehension:
list2 = filter(None, list1)
If None
is used as first argument to filter()
, it filters out every value in the given list, which is False
in a boolean context. This includes empty lists.
It might be slightly faster than the list comprehension, because it only executes a single function in Python, the rest is done in C.