Loading Zone
Loading Zone

Reputation: 197

python how to check if a nested list contain all None values

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

Answers (2)

m.hatami
m.hatami

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

Thierry Lathuille
Thierry Lathuille

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

Related Questions