use*_*131 0 python boolean-logic if-statement list boolean-expression
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")
Run Code Online (Sandbox Code Playgroud)
>>> at least an empty list inside A
Run Code Online (Sandbox Code Playgroud)
Example 2
>>> at least an empty list inside A
Run Code Online (Sandbox Code Playgroud)
>>> no empty list as elements of A
Run Code Online (Sandbox Code Playgroud)
I tried with the condition
A = [[3,2], [5]]
if (condition):
print("no empty list as elements of A")
else:
print("at least an empty list inside A")
Run Code Online (Sandbox Code Playgroud)
but it seems not to work properly. What am I missing?
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")
Run Code Online (Sandbox Code Playgroud)