0

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?

1 Answer 1

5

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")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it's exactly what I was looking for! Do you know why the solution that I tried (end of the question) doesn't work?
(not b for b in A) is a generator expression, which is always true. You aren't actually iterating over it, and if you did, you'd have a list of boolean values, not a single one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.