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.
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...
Fastest way to check if a NumPy array contains n consecutive copies of the same element?
How to remove all values in a list above a certain value?
import numpy as np
arr = np.random.randint(0,30,10)
threshold = 20
mask = arr > threshold
print("arr", arr)
if True in mask:
print("yes, elements above threshold are :", arr[mask])
else:
print("No elements are above threshold")
For a numpy array, use the .any method on a mask instead of looping through the array:
data_np = np.random.randint(0, 110, 10)
if (data_np >= 100).any():
print('yes')