Reputation: 197
I have a list that looks like this
[[None, None, None], [None, None, None]]
How to efficiently check all elements are None?
I know how to do this for
lst = [None, None, None]
all(item is None for item in lst )
Upvotes: 0
Views: 346
Reputation: 603
try this :
def checkList(lstInput):
for i in lst:
for j in i:
if j!=None:
return False
return True
lst=[[None,None,None],[None,None,None]]
print(checkList(lst))
Upvotes: 0
Reputation: 24231
You can do it like this, all
would return False
as soon as it would encounter a value different of None
:
lst = [[None, None, None], [None, None, None]]
all(item is None for sublist in lst for item in sublist)
# True
Upvotes: 3