Python | Check if all the values in a list that are greater than a given value
Given a list, print all the values in a list that are greater than the given value

Examples:
Input : list = [10, 20, 30, 40, 50]
given value = 20
Output : No
Input : list = [10, 20, 30, 40, 50]
given value = 5
Output : YesMethod 1: Traversal of list
By traversing in the list, we can compare every element and check if all the elements in the given list are greater than the given value or not.
Implementation:
Python
# python program to check if all# values in the list are greater# than val using traversaldef check(list1, val): # traverse in the list for x in list1: # compare with all the values # with val if val>= x: return False return True # driver codelist1 =[10, 20, 30, 40, 50, 60]val = 5if(check(list1, val)): print"Yes"else: print"No"val = 20if(check(list1, val)): print"Yes"else: print"No" |
Yes No
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using all() function:
Using all() function we can check if all values are greater than any given value in a single line. It returns true if the given condition inside the all() function is true for all values, else it returns false.
Implementation:
Python
# python program to check if all# values in the list are greater# than val using all() functiondef check(list1, val): return(all(x > val for x in list1)) # driver codelist1 =[10, 20, 30, 40, 50, 60]val = 5if(check(list1, val)): print"Yes"else: print"No"val = 20if (check(list1, val)): print"Yes"else: print"No" |
Yes No
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 3 : Using min() method
Python3
# python program to check if all# values in the list are greater# than vallist1 =[10, 20, 30, 40, 50, 60]val = 5if(min(list1)>=val): print("Yes")else: print("No") |
Yes
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 4: Using any function
One approach is using a generator expression with the any function.
Here is an example of how this can be implemented:
Python3
def check_greater(lst, val): return not any(x <= val for x in lst)list1 = [10, 20, 30, 40, 50]val = 20print(check_greater(list1, val)) # Falseval = 5print(check_greater(list1, val)) # True |
False True
The any function returns True if any element in the generator expression evaluates to True, and False otherwise. The not operator is used to negate the result of any, so that the function returns True if all elements in the list are greater than the given value, and False otherwise.
Method #5:Using filter()+lambda functions
Python3
# python program to check if all# values in the list are greaterdef check(list1, val): return(len(list(filter(lambda x: x > val, list1))) == len(list1))# driver codelist1 = [10, 20, 30, 40, 50, 60]val = 5if(check(list1, val)): print("Yes")else: print("No")val = 20if (check(list1, val)): print("Yes")else: print("No") |
Yes No
Time Complexity : O(N)
Auxiliary Space : O(N)







Please Login to comment...