Home > Back-end >  what is the easiest way to detect if list contains only empty arrays
what is the easiest way to detect if list contains only empty arrays

Time:09-23

what is the easiest way to check a list of arrays to see if the arrays they contain are all empty? for example, this is what the arrays look like and the following output is what I would expect:

a = [[],[]] ==> True
b = [["x"], ["y"], []] ==> False

CodePudding user response:

Presuming that you know that the list only contains other lists (which do not themselves contain lists), this is one way:

>>> not any(a)
True
>>> not any(b)
False

CodePudding user response:

Use any :

print(not any(a))
print(not any(b))

output:

True
False

CodePudding user response:

This can work:

for i in a:
    if len(str(i))>0:
        print("With Something")
    else:
        print("empty")
  • Related