Check if all values in Python List are greater than a value

In this article, we will learn how to check if all elements of a list are greater than a given value in Python.

Table Of Contents

Solution 1 : Using all() function

To verify if all elements in a list are greater than a given value, apply this condition to each element of list and store results in a boolean list. Size of this boolean list will be equal to the size of the original list. Also, a True value in this boolean list tells that the corresponding value in original list is greater than the given value. So, if all elements in this boolean list are True, then it means all elements in original list are greater than given value. Now to check if all elements of a list are True, we can use the all() function. It accepts a sequence of boolean type elements and returns True if all the elements in that sequence evaluates to True.

Let’s see an example, where we will check if all the numbers in list are greater than 30.

# A list of numbers
listOfNumbers = [67, 78, 55, 44, 90, 98, 99, 76]

minValue = 30

# Check if all values in a Python List are greater than minValue i.e. 30
if all(num > minValue  for num in listOfNumbers):
    print('Yes, all elements in List are greater than 30')
else:
    print('No, all elements in List are not greater than 30')

Output:

Advertisements
Yes, all elements in List are greater than 30

Here, we confirmed that all numbers in list were greater than 30.

Solution 2 : Using for-loop

Iterate over all elements of a list using a for loop and check if each element is greater than given value or not. During iteration, if you find any element that is equal to 30 or less than 30, then it means all the elements of the list are not greater than given value. If all evaluates to True, then it means all the elements of the list are greater than 30.

Let’s see an example, where we will check if all the numbers in list are greater than 30.

# A list of numbers
listOfNumbers = [67, 78, 55, 44, 90, 98, 99, 76]

minValue = 30

result = True

# Iterate over all numbers in a list and check 
# if any values in a Python List is less than or
# equal to minValue i.e 30
for num in listOfNumbers:
    if num <= minValue:
        result = False
        break

if result:
    print('Yes, all elements in List are greater than 30')
else:
    print('No, all elements in List are not greater than 30')

Output:

Yes, all elements in List are greater than 30

Here, we confirmed that all numbers in list were greater than 30.

Summary

Today, we learned how to check if all elements of a list are greater than a given value in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top