Home > Net >  Python: condition to check that none of list's element is an empty list
Python: condition to check that none of list's element is an empty list

Time:12-10

I have a list of listA; each element of A is a list. I want a condition (to use for a if statement) that return True if all the element of A are non empty and False otherwise. How can I express this condition?

Example 1

A = [[], [5]]
if (condition):
    print("no empty list as elements of A")
else:
    print("at least an empty list inside A")
>>> at least an empty list inside A

Example 2

A = [[3,2], [5]]
if (condition):
    print("no empty list as elements of A")
else:
    print("at least an empty list inside A")
>>> no empty list as elements of A

I tried with the condition

if(not b for b in A):

but it seems not to work properly. What am I missing?

CodePudding user response:

Since an non-empty list is considered truthy, you can use all:

if all(A):
    print("No empty list in A")
else:
    print("At least one empty list in A")
  • Related