Check if all the values in a list that are greater than a given value Python (original) (raw)

Last Updated : 22 Apr, 2025

We are given a list of numbers and a target value, and our task is to check whether all the elements in the list are greater than that given value. **For example, if we have a list like [10, 15, 20] and the value is 5, then the result should be True because all elements are greater than 5. This is useful when validating data against a minimum threshold.

Using map() with all()

all() function returns True if all items in an iterable are True. When combined with a generator expression, it checks each element in the list to see if it satisfies a condition and if any element fails the condition, all() will return False immediately.

Python `

a = [10, 20, 30] b = 5 # threshold

res = all(x > b for x in a) print(res)

`

**Explanation: all() function with a generator expression evaluate if each element in **a satisfies the condition x > b. If all elements meet the condition, it returns True otherwise, False.

Using filter()

Another way to check this is by using the filter() function. It filters out the values that do not meet the condition and returns a new iterable. We can then check if the length of the filtered list is the same as the original list.

Python `

a = [10, 20, 30] b = 5 # threshold

res = len(list(filter(lambda x: x > b,a))) == len(a) print(res)

`

**Explanation: filter() with a lambda filters elements in a greater than b. If the filtered list’s length equals the original list’s length, it returns True otherwise, False.

Using numpy (for large numeric datasets)

If we working with a large list of numeric values, we can use numpy, which is optimized for vectorized operations and can perform checks faster than standard Python loops.

Python `

import numpy as np a = [10, 20, 30] b = 5

res = np.all(np.array(a) > b) print(res)

`

**Explanation: This code converts a to a NumPy array and uses np.all() to check if all elements are greater than **b. It returns True if all satisfy the condition otherwise, False.

Using Loop

This is the most basic way to check if all values are greater than a given number is by using a loop. We can go through each item in the list and check if it’s greater than the number. If we find any value that is not greater, we stop and return False.

Python `

a = [10, 20, 30] b = 5 # threshold res = True

for x in a: if x <= b: res = False break print(res)

`

**Explanation: This code iterates through a and checks if any element is less than or equal to **b. If it finds such an element, **res is set to False and the loop breaks. If no such element is found, **res remains True.