Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare all items in a list with an integer without using for loop

I have a couple of lists which vary in length, and I would like to compare each of their items with an integer, and if any one of the items is above said integer, it breaks the for loop that it is in.

for list in listoflists:
    if {anyiteminlist} > 70:
        continue    #as in skip to next list

    {rest of code here} 

Basically, I need to say: "If anything in this list is above 70, continue the loop with the next list"

like image 412
rptynan Avatar asked Feb 13 '12 02:02

rptynan


1 Answers

Don't use list as a variable name, it shadows the builtin list(). There is a builtin function called any which is useful here

if any(x>70 for x in the_list):

The part inbetween the ( and ) is called a generator expression

like image 171
John La Rooy Avatar answered Oct 21 '22 05:10

John La Rooy