Member-only story
The any() Function in Python
Learn about the any() function in Python
or operator
Let’s say a job posting only requires one of the following to be true: 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 Falseis_qualified(11, 11, 7)
# Trueis_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…