kaan46
kaan46

Reputation: 83

What is the easiest way to detect if a list contains only empty arrays?

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

Upvotes: 2

Views: 50

Answers (3)

This can work:

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

Upvotes: 0

SomeDude
SomeDude

Reputation: 14228

Use any :

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

output:

True
False

Upvotes: 3

sj95126
sj95126

Reputation: 6908

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

Upvotes: 2

Related Questions