SandyBr SandyBr - 3 months ago 15
Python Question

Python: How to remove empty lists from a list?

I have a list with empty lists in it:

list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText']

How can I remove the empty lists so that I get:

list2 = ['text', 'text2', 'moreText']

I tried list.remove('') but that doesn't work.



list2 = [x for x in list1 if x != []]

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]