Check if several values are in a python list

1

I have a list of integers called MsgID and I want to do an If where it checks if within that list there is any value greater than 3. My attempt:

if [3,2,1,0] not in MsgID:
    pass

I've also tried with

if MsgID > 3:
     pass
    
asked by Juanca M 05.05.2017 в 10:15
source

1 answer

2

Use the Any()

function
>>> MsgID = [3,2,1,0]
>>> any(i > 3 for i in MsgID)
False

>>> MsgID2 = [3,2,1,0, 4]
>>> any(i > 3 for i in MsgID2)
True

Demo

In your code:

if any(i > 3 for i in MsgID):
    pass
    
answered by 05.05.2017 / 10:20
source