
All Python programmers should know what the and and or operators are in Python and how to use them. Sometimes, however, there are better ways to accomplish the same task performed by those operators.
In this article, we will review two such ways: the all() and any() functions, which provide cleaner and simpler code that is easier to read and interpret.
satisfying conditions
Let’s say that there’s a job posting for a Data Science position. This position requires all of the following: at least 10 years of SQL experience, 12 years of machine learning experience, and 8 years of statistical analysis experience. In other words, the typical requirements for an entry-level data science position these days.
and operator
We can write a function that takes in the years of experience a candidate has, and checks whether that candidate satisfies all the requirements by returning True or False, using the and operator:
def is_qualified(sql, ml, stats):
if (sql >= 10) and (ml >= 12) and (stats >= 8):
return True
else:
return False
The above function uses the and operator to check if all conditions or operands are True. If all the expressions evaluate to True, the and operator will return True.
So if a candidate has 11 years of SQL experience, 12 years of machine learning experience, and 9 years of statistical analysis experience, the function will return True:
is_qualified(11, 12, 9)
# True
But if another candidate has 9 years of SQL, 12 years of machine learning, and 9 years of statistical analysis experience, the function will return False:
is_qualified(9, 12, 9)
# False
all() function
Another way to write this function would be to use the all() function **** instead.
all(iterable)
The all() function takes in an iterable object, such as a list, and checks if all the elements within that iterable are Truthy. In other words, if all the elements evaluate to True (or if the iterable is empty), all() will return True.
Remember, Truthy refers to any value that evaluates to True (and Falsey is any value that evaluates to False).
Thus, we can rewrite the above _is_qualified()_ function as follows:
def is_qualified(sql, ml, stats):
requirements = [
sql >= 10,
ml >= 12,
stats >= 8
]
return all(requirements)
We create a list that contains the expressions that need to be True in order for a candidate to be qualified for the position. If all the elements in requirements evaluate to True, then all(requirements) evaluates to True, and True is returned. This is a much cleaner and more intuitive way of writing this function.
or operator
Let’s say another job posting only requires one of the following: at least 10 years of SQL experience, or 12 years of machine learning experience, or 8 years of statistical analysis experience.
We can write a function that checks if any of those are True by using the or operator:
def is_qualified(sql, ml, stats):
if (sql >= 10) or (ml >= 12) or (stats >= 8):
return True
else:
return False
is_qualified(11, 11, 7)
# True
is_qualified(9, 10, 7)
# False
We used the or operator in the above function. The or operator returns True if any of those expressions evaluate to True. If all the expressions evaluate to False, the or operator returns False.
any() function
Again, there is an easier way to do this, and that’s with using the any() function.
any(iterable)
As the name implies, the any() function takes in an iterable, and checks if any of the elements evaluate to True. If at least one of the elements is Truthy (evaluates to True), then it will return True.
The any() function returns False if the iterable is empty. The all() function will return True if the iterable is empty.
def is_qualified(sql, ml, stats):
requirements = [
sql >= 10,
ml >= 12,
stats >= 8
]
return any(requirements)
And that’s it! The any() function will check if any of the elements in requirements evaluate to True. If so, it will return True. If none of the elements evaluate to True, then it will return False.
short-circuiting
Both any() and all() will short-circuit the execution the moment they know what to return. In other words, if the all() function encounters a Falsey value, then it will immediately return False. And if the any() function encounters a Truthy value, then it will immediately return True. Thus, the entire iterable does not always need to be consumed, therefore improving performance.
The reason to why this happens can be seen in the Python implementation of any() and all():
def all(iterable):
for element in iterable:
if not element:
return False
return True
def any(iterable):
for element in iterable:
if element:
return True
return False
In the all() function above, if an element evaluates to False, the function returns False (and thus stops iterating through the iterable). Remember that the return statement will end the execution of a function.
In the any() function above, if an element evaluates to True, the function returns True (and thus stops iterating through the iterable).
If you enjoy reading stories like these and want to support me as a writer, consider signing up to become a Medium member. It’s $5 a month, giving you unlimited access to stories on Medium. If you sign up using my link, I’ll earn a small commission.
I hope this short tutorial on the any() and all() functions was helpful. Thank you for reading!