Check if all values in list are greater than a certain number
Use the all()
function with a generator expression:
>>> my_list1 = [30, 34, 56]>>> my_list2 = [29, 500, 43]>>> all(i >= 30 for i in my_list1)True>>> all(i >= 30 for i in my_list2)False
Note that this tests for greater than or equal to 30, otherwise my_list1
would not pass the test either.
If you wanted to do this in a function, you'd use:
def all_30_or_up(ls): for i in ls: if i < 30: return False return True
e.g. as soon as you find a value that proves that there is a value below 30, you return False
, and return True
if you found no evidence to the contrary.
Similarly, you can use the any()
function to test if at least 1 value matches the condition.
...any reason why you can't use min()
?
def above(my_list, minimum): if min(my_list) >= minimum: print "All values are equal or above", minimum else: print "Not all values are equal or above", minimum
I don't know if this is exactly what you want, but technically, this is what you asked for...