The Wayback Machine - https://web.archive.org/web/20230202180612/https://www.geeksforgeeks.org/python-check-values-list-greater-given-value/
Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 27 Jan, 2023
Improve Article
Save Article

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 : Yes

Method 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 traversal
 
def 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 code
list1 =[10, 20, 30, 40, 50, 60]
val = 5
if(check(list1, val)):
    print"Yes"
else:
    print"No"
 
val = 20
if(check(list1, val)):
    print"Yes"
else:
    print"No"

Output

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() function
 
def check(list1, val):
    return(all(x > val for x in list1))
     
     
# driver code
list1 =[10, 20, 30, 40, 50, 60]
val = 5
if(check(list1, val)):
    print"Yes"
else:
    print"No"
 
val = 20
if (check(list1, val)):
    print"Yes"
else:
    print"No"

Output

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 val
list1 =[10, 20, 30, 40, 50, 60]
val = 5
if(min(list1)>=val):
    print("Yes")
else:
    print("No")

Output

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 = 20
print(check_greater(list1, val)) # False
 
val = 5
print(check_greater(list1, val)) # True

Output

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 greater
 
 
def check(list1, val):
    return(len(list(filter(lambda x: x > val, list1))) == len(list1))
 
 
# driver code
list1 = [10, 20, 30, 40, 50, 60]
val = 5
if(check(list1, val)):
    print("Yes")
else:
    print("No")
val = 20
if (check(list1, val)):
    print("Yes")
else:
    print("No")

Output

Yes
No

Time Complexity : O(N)
Auxiliary Space : O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!