Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python assert all elements in list is not none [duplicate]

Tags:

python

assert

I was wondering if we could assert all elements in a list is not None, therefore while a = None will raise an error.

The sample list is [a, b, c]

I have tried assert [a, b, c] is not None, it will return True if any one of the elements is not None but not verifying all. Could you help figure it out? Thanks!!

like image 886
PPP Avatar asked Jul 19 '26 18:07

PPP


2 Answers

Unless you have a weird element that claims it equals None:

assert None not in [a, b, c]
like image 190
no comment Avatar answered Jul 21 '26 07:07

no comment


Do you mean by all:

>>> assert all(i is not None for i in ['a', 'b', 'c'])
>>>

Or just simply:

assert None not in ['a', 'b', 'c']

P.S just noticed that @don'ttalkjustcode added the above.

Or with min:

>>> assert min(a, key=lambda x: x is not None, default=False)
>>>
like image 22
U12-Forward Avatar answered Jul 21 '26 07:07

U12-Forward