False match with 0 in python list

this line evaluates to true in python

False in [0,1,2]

because Falseand 0are after a cast. Is there any way to avoid this type conversion? Something like an operator ===for a list?

(I know that I can handle this case with a loop by explicitly checking the types of values, but I'm curious if there is a short and nice trick to do this without a loop).

+6
source share
3 answers

If you really feel the need to do the same, you can do the following.

False in filter(lambda x: isinstance(x, bool), [0, 1, 2])

Or as suggested by @JonClements

any(x is False for x in [0, 1, 3]) # Since immutable values (including 
                                   # boolean) are instantiated only once.

, 0 False, falsy Python. , .

+8

, Python . False == 0 , bool int, .

, ===, , :

lst = [....]
testvalue = False
if any(testvalue == elem and type(testvalue) is type(elem) for elem in lst):
    # lst contains testvalue and it is the same type

, , .

, . in , in , any() , True, .

, float. 0.0 == 0 , , , . , Decimal():

>>> 0j == 0 == 0.0 == Decimal('0')
True

bool - , 0 1.

, - hinting ; , , , .

+10

"".

>>> False == 0
True
>>> False is 0
False
+1

All Articles