In python data analysis, we sometime face a situation where we need to compare a given number with a list containing many values. In this article we need to fins if a given number is less than each of the values present in a given list. We are going to achieve it using the following two ways.
Using for loop
We iterate through the given list and compare the given value with each of the values in the list. Once all values from the list are compared and the comparison condition holds good in each of the step, we print out the result as Yes. Else the result is a No.
Example
List = [10, 30, 50, 70, 90] value = 95 count = 0 for i in List: if value <= i: result = False print("No") break else: count = count +1 if count == len(List): print("yes")
Output
Running the above code gives us the following result −
yes
Using all()
The all method behaves like a loop and compares each element of the list with the given element. So we accomplish the comparison by just using all in an if else condition.
Example
List = [10, 30, 50, 70, 90] value = 85 if (all(x < value for x in List)): print("yes") else: print("No")
Output
Running the above code gives us the following result −
No